Reversing Single Linked List
Reversing Single Linked List
Linked List
Single Linked List: Reversing
Reversing a list can be performed in two ways:
• Iterative method
• Recursive method
Step 3: Move the head node to its next node i.e. head = head->next.
Reversing a List
Step 4: Now, re-connect the current node to its previous node
i.e. curNode->next = prevNode;
Step 5: Point the previous node to current node and current node to head node. Means they
should now point to prevNode = curNode; and curNode = head.
Reversing a List
Step 6: Repeat steps 3-5 till head pointer becomes NULL.
Step 7: Now, after all nodes has been re-connected in the reverse order. Make the last node
as the first node. Means the head pointer should point to prevNode pointer.
• Perform head = prevNode; And finally you end up with a reversed linked list of
its original.
Reversing a List: Iterative Method
Struct node* reverseList(struct node*h)
{
struct node *prevNode, *curNode;
if(h != NULL)
{
prevNode = h;
curNode = h->next;
h = h->next;
while(h != NULL)
{
h = h->next;
curNode->next = prevNode;
prevNode = curNode;
curNode = h;
}