Skip to content

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 

README.md

Reverse a Linked List

Reverse the direction of all pointers in a singly linked list in-place.  ⏱ Time O(n) Β πŸ’Ύ Space O(1)

🧠 The Idea

Use three pointers: prev, curr, and next. For each node, save the next pointer, reverse the current pointer to point backwards, then advance all three pointers forward. When curr is None, prev is the new head.

Algorithm:
  prev, curr = None, head
  while curr:
      next = curr.next    ← save before overwriting
      curr.next = prev    ← reverse the pointer
      prev = curr         ← advance prev
      curr = next         ← advance curr
  return prev             ← new head

πŸ“Š Visual

Original:   A β†’ B β†’ C β†’ D β†’ None

Step 1 (curr=A): save B, A.next=None,  prev=A, curr=B
  None ← A   B β†’ C β†’ D β†’ None

Step 2 (curr=B): save C, B.next=A,     prev=B, curr=C
  None ← A ← B   C β†’ D β†’ None

Step 3 (curr=C): save D, C.next=B,     prev=C, curr=D
  None ← A ← B ← C   D β†’ None

Step 4 (curr=D): save None, D.next=C,  prev=D, curr=None
  None ← A ← B ← C ← D

Return prev=D β†’ new head!  D β†’ C β†’ B β†’ A β†’ None βœ“

πŸ“œ History & Background

Linked list reversal is one of the most iconic interview problems. It appears in nearly every data structures curriculum and is often the first "pointer manipulation" problem taught. The iterative O(1) space solution is a classic test of pointer fluency.

πŸ’Ό Tech Interview Tips

  • Iterative (shown above): O(1) space β€” preferred in interviews
  • Recursive: O(n) stack space β€” elegant but risk of stack overflow
  • Always save curr.next BEFORE overwriting curr.next
  • To reverse a portion (LeetCode 92): find the sublist boundaries, reverse, then reconnect
  • Follow-ups: reverse in groups of K (LeetCode 25), check if list is palindrome (reverse second half)

🎯 Common Use Cases

  • Undo/redo operations (reverse action history)
  • Browser back navigation
  • Text editor cursor movement
  • Transaction rollback sequences

πŸ”— Related Problems