## Sunday, May 15, 2011

This is a short post but this topic deserved it’s own post. As I had mentioned before, the order in which you manipulate the pointers is extremely critical. Reversing a linked list can become quite complicated if you do not pay close attention to the order.

Consider the following linked list made up of 4 nodes. The head pointer points to the start of the list.

To reverse this list, we need 3 additional pointers.  We need a currentNode pointer to traverse through the list, a nextNode pointer to save the next node in the list,  and finally, a prevNode pointer to keep track of the last node in the new list as we keep on reversing the input list. So, in essence, here is the pseudo algorithm to reverse the list:

• Initialize currentNode pointer to the start of the list and prevNode to NULL (as the new list is currently pointing to NULL).
• While currentNode is not NULL
• Save the next node in nextNode
• Set the currentNode to point to the prevNode.
• Move the prevNode to the currentNode.
• Move the currentNode pointer to the nextNode.

Look at the following representations to better understand the concept outlined.

#### Step 3: Move the pointers

Add the process continues till the list has been reversed. As the last step, you need to reset the head pointer to point to the prevNode as that is now the current head of the reversed list. Once you have understood the logic, the code part is actually quite simple as shown in the function below.

`void Reverse(){    // Initialize currentNode pointer to the start of the list     // and prevNode to NULL     // (as the new list is currently pointing to NULL).    Node *currentNode = head;    Node *prevNode = NULL;    Node *nextNode = NULL;    while (currentNode != NULL)    {        // Save the next node in nextNode         nextNode = currentNode->next;        // Set the currentNode to point to the prevNode.          currentNode->next = prevNode;        // Move the prevNode to the currentNode.        prevNode = currentNode;        // Move the currentNode pointer to the nextNode.        currentNode = nextNode;    }    // reset the head pointer to point to the prevNode     // as that is now the current head of the reversed list    head = prevNode;}`

1. Thanks Nikhil, your blog is a great resource.

VR

2. Perhaps a recursive solution?

end

3. one of the best explainations so far.. (y)

4. Hello There,

Thanks for highlighting this and indicating about Programming Interview Questions and Answers where more study and thought is necessary.

I started to program with C and have some programming in JAVA. However, I have a small understanding problem with the following simple code below.
I understand that at the command "while (isspace(ch=getc(in)))" the programm reads till if founds a character that is either a " or a blank. This symbol is assigned to the variable delim.
Is it then correct to say that the program starts again at the same position as before and continues to read the string till "((ch=getc(in))!=delim)" is fulfilled?
THANK YOU!! This saved my butt today, I’m immensely grateful.

Gracias,
Pallavi