206. Reverse Linked List
At a Glance
- Topic: linked-list
- Pattern: In-place Linked List Reversal
- Difficulty: Easy
- Companies: Amazon, Google, Meta, Bloomberg, Microsoft
- Frequency: Very High
- LeetCode: 206
Problem (one-liner)
Given the head of a singly linked list, reverse every pointer direction and return the new head (previously the tail).
Recognition Cues
- “Reverse a linked list”
- In-place,
O(1)extra space - Iterative or recursive reconstruction of
Nextfields
Diagram
At-a-glance flow (replace with problem-specific Mermaid as you refine this note). camelCase node IDs; no spaces in IDs.
Loading diagram…
Approaches
- Brute force —
O(n)time /O(n)space — build a new list or copy to slice then relink. - Better — same as optimal for this task.
- Optimal —
O(n)time /O(1)space — three pointers:previous,current,nextto relink in one pass.
Optimal Solution
Go
type ListNode struct {
Val int
Next *ListNode
}
func reverseList(head *ListNode) *ListNode {
var previous *ListNode
current := head
for current != nil {
next := current.Next
current.Next = previous
previous = current
current = next
}
return previous
}JavaScript
class ListNode {
constructor(val = 0, next = null) {
this.val = val;
this.next = next;
}
}
function reverseList(head) {
let previous = null;
let current = head;
while (current !== null) {
const next = current.next;
current.next = previous;
previous = current;
current = next;
}
return previous;
}Walkthrough
List 1 -> 2 -> 3 -> nil
| step | previous | current | next | action |
|---|---|---|---|---|
| init | nil | 1 | 2 | — |
| 1 | 1 | 2 | 3 | 1.Next = nil |
| 2 | 2 | 3 | nil | 2.Next = 1 |
| 3 | 3 | nil | — | return previous=3 |
Result 3 -> 2 -> 1. Invariant: reversed prefix hangs off previous.
Edge Cases
- Empty list —
nilhead. - Single node — returns same node with
Nextnil.
Pitfalls
- Losing the rest of the list — stash
nextbefore rewritingcurrent.Next. - Returning old
headinstead of new head (previousafter loop).
Similar Problems
- 92. Reverse Linked List II — reverse only a subrange.
- 234. Palindrome Linked List — reverse half to compare.
- 143. Reorder List — reverse second half then weave.
Variants
- Reverse between indices — pattern becomes range reversal.
- Recursive version —
O(n)call stack; elegant but less space-optimal.
Mind-Map Tags
#linked-list #in-place #three-pointer #reversal
Last updated on
Spotted something unclear or wrong on this page?