# Reverse a Singly Linked List in a Single Iteration

Suppose we have a linked list and we want to reverse it in Single iteration and O(n) complexity.

Our Node structure is,

```struct node
{
Â Â  Â int value;
Â Â  Â node * nextPtr;Â Â  Â
Â Â  Â node(int val)
Â Â  Â {
Â Â  Â Â Â  Â value = val;
Â Â  Â Â Â  Â nextPtr = NULL;Â Â  Â
Â Â  Â }
};
```

Now we will create a function to reverse it.

This function will accept 2 parameters i.e.
1.) Node to reverse
2.) Pevious node pointer.

We will call this function with default value for last parameter i.e.

```Â Â  Â node * ptr = createListFromArray(arr, sizeof(arr)/sizeof(int));
Â Â  Â ptr = reverseList(ptr);
```

Inside this reverseList function,

If given pointer ptr is NULL it means you have reached the end of linked list, so just return the previous
node because that will be the last Node.
Store the next pointer of passed ptr i.e. ptr->nextPtr in a variable and make ptr->nextPtr to passed previous node.

### Best Resources to Learn C++:

Now again call the reverseList with next node pointer stored earlier and current node as the previous node.
In the end return last node of original list because now it will be root node of reversed linked list.

Here is the reverse function code,

```node * reverseList(node * ptr, node * prevNode = NULL)
{
Â Â  Â if(!ptr)
Â Â  Â Â Â  Â return prevNode;
Â Â  Â node * pNext = ptr->nextPtr;
Â Â  Â ptr->nextPtr = prevNode;
Â Â  Â return reverseList(pNext, ptr);
}
```

Full Code example is as follows,

[code language=”cpp”]
#include <iostream>
/*
Â * Node will contain and int variable as value
Â * and a node pointer to next Node.
Â **/
struct node
{
Â Â  Â int value;
Â Â  Â node * nextPtr;Â Â  Â
Â Â  Â node(int val)
Â Â  Â {
Â Â  Â Â Â  Â value = val;
Â Â  Â Â Â  Â nextPtr = NULL;Â Â  Â
Â Â  Â }
};

/*
*Â  If given pointer ptr is NULL it means you have reached
*Â  the end of linked list, so just return the previous
*Â  node because that will be the last Node.
* Â
*Â  Store the next pointer of passed ptr i.e. ptr->nextPtr
*Â  in a variable and make ptr->nextPtr to passed previous node.
* Â
*Â  Now again call the reverseList with next node pointer stored
*Â  earlier and current node as the previous node.
* Â
*Â  In the end return last node of original list because now it
*Â  will be root node of reversed linked list.
**/
node * reverseList(node * ptr, node * prevNode = NULL)
{
Â Â  Â
Â Â  Â if(!ptr)
Â Â  Â Â Â  Â return prevNode;
Â Â  Â node * pNext = ptr->nextPtr;
Â Â  Â ptr->nextPtr = prevNode;
Â Â  Â return reverseList(pNext, ptr);
}

/*
Â * Iterate the passed array one by one and create a node
Â * for each element and append it in last node’s next pointer.
Â * Also keep a track of last node created while iteration to
Â * update its next pointer in next iteration.
Â * Also store the first node created,that we will returned as
Â * root node in end.
**/
node * createListFromArray(int * ptr, int arraySize)
{
Â Â  Â node * nodePtr = NULL;
Â Â  Â node * rootNodePtr = NULL;
Â Â  Â node * lastNodePtr = NULL;
Â Â  Â for(int i = 0 ; i < arraySize; i++)
Â Â  Â {
Â Â  Â Â Â  Â if(!nodePtr)
Â Â  Â Â Â  Â {
Â Â  Â Â Â  Â Â Â  Â nodePtr = new node(*(ptr+i));
Â Â  Â Â Â  Â Â Â  Â if(!rootNodePtr)
Â Â  Â Â Â  Â Â Â  Â Â Â  Â rootNodePtr = nodePtr;Â Â  Â
Â Â  Â Â Â  Â Â Â  Â if(lastNodePtr)Â Â  Â
Â Â  Â Â Â  Â Â Â  Â Â Â  Â lastNodePtr->nextPtr = nodePtr;
Â Â  Â Â Â  Â }
Â Â  Â Â Â  Â lastNodePtr = nodePtr;
Â Â  Â Â Â  Â nodePtr = nodePtr->nextPtr;
Â Â  Â }
Â Â  Â return rootNodePtr;
}
/*
Â * Store the next pointer of passed node as temp variable.
Â * Delete the current pointer and pass the earlier stored next pointer
Â * to destroyList funtion.
Â *
Â * If ptr is null it means its the end of linked list, so just return
Â * because complete linked list is deleted.
Â **/
void destroyList(node * ptr)
{
Â Â  Â if(ptr)
Â Â  Â {
Â Â  Â Â Â  Â node * pNext = ptr->nextPtr;
Â Â  Â Â Â  Â delete ptr;
Â Â  Â Â Â  Â destroyList(pNext);
Â Â  Â }
}
/*
Â * Iterate through all nodes and display content
Â * of each node untill end of linked list is reached.
Â **/
{
Â Â  Â while(ptr != NULL)
Â Â  Â {
Â Â  Â Â Â  Â std::cout<<ptr->value<<" ";
Â Â  Â Â Â  Â ptr = ptr->nextPtr;
Â Â  Â }
Â Â  Â std::cout<<std::endl;
}
/*
Â * Testing functions.
Â **/
int main()
{
Â Â  Â int arr[] = {1,2,3,4, 89,1,1,1,0};
Â Â  Â node * ptr = createListFromArray(arr, sizeof(arr)/sizeof(int));
Â Â  Â ptr = reverseList(ptr);
Â Â  Â destroyList(ptr);
Â Â  Â return 0;
}

[/code]

### 2 thoughts on “Reverse a Singly Linked List in a Single Iteration”

1. I just have one remark. Using recursion could cause stack overflow. Available stack size could be significanly smaller than the length of heap allocated array.

2. @Zbigniew: Could be the case if the list is really really long. But in most cases it should be fine though. But its a good thought since we can implement the same in an iterative way. A loop over the same code traversing the list should do it. ðŸ™‚

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top