Reverse linked list

I am trying to create a function to reverse a linked list.  The function should have one parameter which is a head pointer to the linked list.. The function should modify the linked list pointed to by its parameter by reversing the order of the items in the list....

Could someone give me some insight on this?  This is homework so I am not really looking for code, but algorithm information would be helpful...  I have been staring at this problem for far too long now and I know it isn't very difficult...
Who is Participating?
Infinity08Connect With a Mentor Commented:
Just traverse the linked list from beginning to end, and invert the links.

Suppose you have a list :

    elem1 -> elem2 -> elem3 -> ...

Then you start at the beginning :

    elem1 -> elem2 -> elem3 -> ...

And come to this situation :

    elem1 <- elem2 -> elem3 -> ...

And then you can do the same thing for the link between elem2 and elem3, etc.

Does that get you started ?

Can you give it a try, and post some code ?
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> I am trying to create a function to reverse a linked list.  
I assume it is a singly linked list. If so, you should nevertheless add a member pointer for the tail node and not only the head node cause a tail node has a lot of advantages. So, you can add at the front or back and for the task to reverse the list you need the pointer to the tail node as well.

Reversing a linked list mostly is done by calling a recursive member function of the node strcut/class. It  takes a pointer to the previous node as argument. The implementation of the recursive function firstly checks whether the next pointer is not NULL. If so, it calls the next->reverse function  recursively usiing the this as argument: After the call it assigns the parenet pointer to the next pointer:

    1  -> 2 -> 3 -> 4 -> 5

you would call 1->reverse passing NULL and 1 is the head node pointer. The headpointer would call 2->reverse passing this what is 1 what is the head pointer. (2)->reverse would call (3)->reverse(2) passing node 2,  (3) ->reverse would call (4)-reverse(5) because (4)->next == 5. (5)->reverse would stop recursion cause (5)->next is NULL. (5)->reserve would assign (4) to (5)->next and return. (4)->reserve would continue (after 5->reserve() has returned) and would assign (3) top (4)->next, ... and so on, (1)->reverse() would assign NULL to (1)->next, what means that (1) is the new tail now.

Finally you have

   (1) <- (2) <- (3) <- (4) <- (5)

what is a reverse list. The list becomes valid after you exchanged the head node pointer by the former tail node pointer. That's why it would be good if you already have both head and tail. If not, you either need to get the tail node by an additional loop prior to calling the reverse function or you let the reverse function return a node pointer wich was assigned by the very last recursive call when the enxt pointer was NULL. Then the reverse function returns this and all other reverse calls return the same pointer so that finally the linked list has the new head pointer:

class LinkedList
    Node* head;

    void reverse() { if (head) head = head->reverse(NULL); }

Regards, Alex
As it is the homework you can try to realize some diferent algortihms or at least thinking about.
I think, the quickest way is a C-Style algorithm where you scan a list via loop or recursivelly.
In C++ style, in my opinion, it must be an iterator class for scan the list. In addition you must not forgot about the root. If before start it pointed to original head, then after end it must be pointed to original tail.
For imagination, add one fictive NULL item "before" head, then swap the pointers /a linked to b, then b must be linked to a/ and go to the next item. You see, you must to work with two items simultaneously and get the next item before you swap the links.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.