How to Reverse a Linked List: Iterative, Recursive and Interview Variations
Learn to reverse a singly linked list step by step: the three-pointer iterative method, the recursive approach, complexity analysis, reversing a sublist, reversing in groups of k and checking palindromes.
"Reverse a linked list" is one of the most frequently asked coding interview questions. It looks simple, but it tests whether you can manipulate pointers carefully without losing track of nodes. Once you master it, a whole family of harder linked-list problems becomes approachable.
Linked list basics#
A singly linked list is a chain of nodes. Each node stores a value and a reference to the next node. The last node points to null/None.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def build(values):
head = None
for v in reversed(values):
head = ListNode(v, head)
return head
def to_list(head):
out = []
while head:
out.append(head.val)
head = head.next
return outGoal: turn 1 → 2 → 3 → 4 → 5 → None into 5 → 4 → 3 → 2 → 1 → None.
Approach 1: iterative with three pointers#
Walk through the list once and flip each arrow. You need three pointers:
prev: the already-reversed part (starts asNone).curr: the node being processed.nxt: saves the rest of the list before you break the link.
def reverse_list(head: ListNode | None) -> ListNode | None:
prev, curr = None, head
while curr:
nxt = curr.next # 1. save the rest
curr.next = prev # 2. flip the arrow
prev = curr # 3. move prev forward
curr = nxt # 4. move curr forward
return prev # prev is the new head
print(to_list(reverse_list(build([1, 2, 3, 4, 5])))) # [5, 4, 3, 2, 1]Trace#
| Step | prev | curr | Reversed so far |
|---|---|---|---|
| Start | None | 1 | (empty) |
| 1 | 1 | 2 | 1 → None |
| 2 | 2 | 3 | 2 → 1 → None |
| 3 | 3 | 4 | 3 → 2 → 1 → None |
| 4 | 4 | 5 | 4 → 3 → 2 → 1 → None |
| 5 | 5 | None | 5 → 4 → 3 → 2 → 1 → None |
Java and JavaScript#
public ListNode reverseList(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}function reverseList(head) {
let prev = null, curr = head;
while (curr) {
[curr.next, prev, curr] = [prev, curr, curr.next]; // one-line swap
}
return prev;
}Approach 2: recursive#
Trust recursion to reverse everything after the head, then attach the head at the end:
def reverse_recursive(head):
if head is None or head.next is None:
return head # empty or single node
new_head = reverse_recursive(head.next) # reverse the rest
head.next.next = head # the node after head now points back
head.next = None # head becomes the tail
return new_headFor 1 → 2 → 3: the call on 2 → 3 returns 3 → 2 with node 2 still reachable from node 1. Setting 1.next.next = 1 makes 2 → 1, and 1.next = None finishes the list.
Complexity#
| Approach | Time | Space |
|---|---|---|
| Iterative | O(n) | O(1) |
| Recursive | O(n) | O(n) call stack |
The recursive version uses one stack frame per node. Python's default recursion limit is about 1,000, so a long list will crash. In interviews, mention this and prefer the iterative solution.
Common mistakes#
- Losing the rest of the list: flipping
curr.nextbefore saving it leaves no way forward. - Returning
headinstead ofprev: the old head is now the tail. - Forgetting
head.next = Nonein the recursive version, which creates a cycle between the first two nodes. - Not handling empty or one-node lists.
Variation 1: reverse a sublist (positions left to right)#
def reverse_between(head, left, right):
dummy = ListNode(0, head)
before = dummy
for _ in range(left - 1):
before = before.next # node just before the sublist
prev, curr = None, before.next
for _ in range(right - left + 1):
nxt = curr.next
curr.next = prev
prev, curr = curr, nxt
before.next.next = curr # old sublist start → node after sublist
before.next = prev # node before → new sublist start
return dummy.next
print(to_list(reverse_between(build([1, 2, 3, 4, 5]), 2, 4))) # [1, 4, 3, 2, 5]A dummy node placed before the head removes special cases when the head itself might change. It's useful in almost every linked list problem.
Variation 2: reverse nodes in groups of k#
def reverse_k_group(head, k):
dummy = ListNode(0, head)
group_prev = dummy
while True:
kth = group_prev
for _ in range(k): # find the k-th node
kth = kth.next
if not kth:
return dummy.next # fewer than k left: stop
group_next = kth.next
prev, curr = group_next, group_prev.next
while curr is not group_next: # reverse this group
nxt = curr.next
curr.next = prev
prev, curr = curr, nxt
first = group_prev.next # becomes the group's tail
group_prev.next = kth
group_prev = first
print(to_list(reverse_k_group(build([1, 2, 3, 4, 5]), 2))) # [2, 1, 4, 3, 5]Variation 3: palindrome linked list#
Find the middle with fast and slow pointers, reverse the second half, then compare:
def is_palindrome(head):
slow = fast = head
while fast and fast.next:
slow, fast = slow.next, fast.next.next
second = reverse_list(slow)
first = head
while second:
if first.val != second.val:
return False
first, second = first.next, second.next
return True
print(is_palindrome(build([1, 2, 2, 1]))) # TrueThis runs in O(n) time and O(1) extra space.
Practice problems#
- Reverse Linked List (LeetCode 206)
- Reverse Linked List II (92)
- Palindrome Linked List (234)
- Reorder List (143)
- Reverse Nodes in k-Group (25)
- Add Two Numbers II (445)
Analyse your solutions with Big O notation, and compare linked lists with arrays in the Java collections guide (ArrayList vs LinkedList).
Key takeaways#
- The iterative solution uses
prev,currandnextpointers and runs in O(n) time, O(1) space. - Always save
curr.nextbefore changing it, and returnprevas the new head. - The recursive solution is elegant but uses O(n) stack space.
- A dummy node simplifies problems where the head can change.
- The same technique powers reverse-between, k-group reversal and palindrome checks.