Reverse the order of a linked list’s existing nodes by changing their next references.
Problem
Input. The head of a finite acyclic singly linked list, with permission to change its next links.
Output. Return the new head after reversing the original nodes’ order.
Rules. Reuse every node and retain its value; do not copy values into replacement nodes.
The former head becomes the tail and must point to None.
Repeated values are allowed. An empty list returns None; a singleton returns the same node.
Example. 1 -> 2 -> 3 -> 4 -> None becomes 4 -> 3 -> 2 -> 1 -> None.
The returned head is the original node valued 4.
Picture
Maintain two disjoint regions containing every original node exactly once:
previousheads the reversed prefix.headis the current node heading the untouched suffix.
Initially, the prefix is empty and the suffix contains all four nodes.
Save node 2, set 1.next to None, then move previous to 1 and head to 2.
After the next iteration, the regions are previous: 2 -> 1 -> None and head: 3 -> 4 -> None.
Redirecting the current node’s edge cannot lose the suffix because next_node still references its original successor.
Moving both working references restores the same two-region invariant with one more reversed node.
When head becomes None, previous heads the complete result.
Problem trace
Full list: advance the disjoint reversed-prefix and untouched-suffix boundary
- previous
- null
- actual links
- 1->2, 2->3, 3->4, 4->null
- node 0
- 1
- second label
- not shown→previous
- reversed prefix
- not shown→1->null
- unreversed suffix
- not shown→2->3->4->null
- reversed prefix
- 1->null→2->1->null
- unreversed suffix
- 2->3->4->null→3->4->null
- saved next
- 2→3
- reversed prefix
- 2->1->null→3->2->1->null
- unreversed suffix
- 3->4->null→4->null
- saved next
- 3→4
- current
- not shown→null
- actual links
- not shown→4->3, 3->2, 2->1, 1->null
- second label
- previous→not shown
- result
- not shown→4 -> 3 -> 2 -> 1
- current
- null→not shown
- actual links
- 4->3, 3->2, 2->1, 1->null→not shown
- Recognize it
- Use it when a forward-only chain must reverse direction in constant extra space and changing current.next would otherwise destroy access to the unprocessed suffix.
- Keep true
- Before each iteration, previous heads a fully reversed prefix, head heads the untouched forward suffix, and together those disjoint regions contain every original node exactly once.
- Reuse it
- Before mutating the only route to remaining work, save that route; then rewire and advance the boundary. This applies to list splicing, segment reversal, and in-place pointer transformations.
Reference drawing
Problem trace
Save the next node before overwriting the forward link
Execution trace
- previous
- null
- actual links
- 1->2, 2->3, 3->4, 4->null
- node 0
- 1
- second label
- not shown→previous
- reversed prefix
- not shown→1->null
- unreversed suffix
- not shown→2->3->4->null
- reversed prefix
- 1->null→2->1->null
- unreversed suffix
- 2->3->4->null→3->4->null
- saved next
- 2→3
- reversed prefix
- 2->1->null→3->2->1->null
- unreversed suffix
- 3->4->null→4->null
- saved next
- 3→4
- current
- not shown→null
- actual links
- not shown→4->3, 3->2, 2->1, 1->null
- second label
- previous→not shown
- result
- not shown→4 -> 3 -> 2 -> 1
- current
- null→not shown
- actual links
- 4->3, 3->2, 2->1, 1->null→not shown
- Recognize it
- Use it when a forward-only chain must reverse direction in constant extra space and changing current.next would otherwise destroy access to the unprocessed suffix.
- Keep true
- Before each iteration, previous heads a fully reversed prefix, head heads the untouched forward suffix, and together those disjoint regions contain every original node exactly once.
- Reuse it
- Before mutating the only route to remaining work, save that route; then rewire and advance the boundary. This applies to list splicing, segment reversal, and in-place pointer transformations.
Code
The platform supplies ListNode; use this definition when running all blocks together locally.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverse_list(head: ListNode | None) -> ListNode | None:
previous = None
while head:
next_node = head.next
head.next = previous
previous = head
head = next_node
return previous
Each node crosses the boundary once, giving link work and extra references. The old head becomes the null-ended tail; payload values stay attached to their original nodes.
Failure Check
Writing head.next = previous before saving the successor can destroy the only route to the untouched suffix.
Returning the exhausted head gives None, not the new head.
Reversing twice must restore original identity order and all original links, not merely the same values.
Change to a Reversed Copy
New task: Preserve every original link and allocate an independent result chain.
Picture and change: Read the original forward, prepending one copied value to the new chain at each step.
def reversed_copy(head):
copied = None
while head:
copied = ListNode(head.val, copied)
head = head.next
return copied
Check: The example returns new nodes with values [4,3,2,1]; the original still links [1,2,3,4].
No output node belongs to the input.
Payloads are assigned, not recursively copied.
Time remains linear; output allocation becomes nodes.
Use when: Input ownership forbids destructive rewiring.
Change to Recursive Control
New task: Express reversal through call-return structure, with safely bounded input depth.
Picture and change: Reverse the suffix first.
Point the head’s original successor back to the head, then clear head.next.
def reverse_recursive(head):
if head is None or head.next is None:
return head
new_head = reverse_recursive(head.next)
head.next.next = head
head.next = None
return new_head
Check: [1,2,3] returns the same nodes as [3,2,1]; empty input returns None.
Omitting head.next = None leaves a two-node cycle during unwinding.
This implementation uses call-stack space and can exceed Python’s recursion limit.
Use when: A bounded chain and recursive reasoning justify the additional stack cost.
Transfer Check
What must an interleaving operation preserve before changing links in two independent chains?
Answer
Both unconsumed continuations. One saved successor protects one-chain reversal, but not two independently advancing sources.