How to Reverse a Linked List: Simple Explanation and Code
To reverse a
linked list, you change the direction of the next pointers of each node so they point to the previous node instead of the next. This is done by iterating through the list and updating pointers until all nodes are reversed, resulting in the last node becoming the new head.Syntax
The basic syntax to reverse a singly linked list involves three pointers:
- prev: Tracks the previous node, starts as
null. - current: Tracks the current node being processed.
- next: Temporarily stores the next node to move forward safely.
In each step, you set current.next = prev, then move prev and current forward until current is null. Finally, prev will be the new head of the reversed list.
pseudo
prev = null current = head while current is not null: next = current.next current.next = prev prev = current current = next head = prev
Example
This example shows how to reverse a singly linked list in Python. It creates a list, reverses it, and prints the result.
python
class Node: def __init__(self, value): self.value = value self.next = None def print_list(head): current = head values = [] while current: values.append(str(current.value)) current = current.next print(' -> '.join(values)) def reverse_linked_list(head): prev = None current = head while current: next_node = current.next current.next = prev prev = current current = next_node return prev # Create linked list 1 -> 2 -> 3 -> 4 head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) print('Original list:') print_list(head) # Reverse the list head = reverse_linked_list(head) print('Reversed list:') print_list(head)
Output
Original list:
1 -> 2 -> 3 -> 4
Reversed list:
4 -> 3 -> 2 -> 1
Common Pitfalls
Common mistakes when reversing a linked list include:
- Not saving the next node before changing
current.next, which causes loss of the rest of the list. - Forgetting to update the head to the new first node after reversal.
- Confusing
prevandnextpointers, leading to cycles or broken links.
Always keep a temporary pointer to the next node before reversing the link.
python
def wrong_reverse(head): current = head while current: current.next = current.prev # Incorrect: 'prev' attribute does not exist current = current.next return head # Correct approach uses a separate 'prev' variable and saves next node first.
Quick Reference
Remember these key points when reversing a linked list:
- Use three pointers:
prev,current, andnext. - Update
current.nexttoprevat each step. - Move
prevandcurrentforward untilcurrentisnull. - Set the head to
prevafter the loop ends.
Key Takeaways
Reverse a linked list by changing each node's next pointer to the previous node.
Always save the next node before changing pointers to avoid losing the list.
Use three pointers: prev, current, and next to track nodes during reversal.
After reversal, update the head to the last processed node (prev).
Common errors include losing nodes or creating cycles by incorrect pointer updates.