By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This problem appears in 1 out of every 3 onsite interviews—mastering it proves you can manipulate pointers, handle edge cases, and think recursively—skills that separate strong candidates from the rest."
Before tackling this problem, ensure you understand: 1. Linked List Basics – Node structure (val, next), traversal, and pointer manipulation. 2. Recursion Fundamentals – Call stack, base cases, and recursive decomposition. 3. Time & Space Complexity – Big-O notation for iterative vs. recursive approaches.
val
next
prev
curr
head is None or head.next is None
dummy = ListNode(0)
Repeat this mental checklist for every linked list reversal problem:
prev = None
curr = head
next = None
None
next = curr.next
curr.next = prev
prev = curr
curr = next
head is None
head.next is None
head
new_head = reverseList(head.next)
head.next.next = head
head.next = None
new_head
Problem: Reverse a singly linked list iteratively. Input: 1 -> 2 -> 3 -> 4 -> 5 -> None Output: 5 -> 4 -> 3 -> 2 -> 1 -> None
1 -> 2 -> 3 -> 4 -> 5 -> None
5 -> 4 -> 3 -> 2 -> 1 -> None
curr = 1
next = 2
1.next = None
prev = 1
curr = 2
next = 3
2.next = 1
prev = 2
curr = 3
curr = None
5
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def reverseList(head: ListNode) -> ListNode: prev, curr = None, head while curr: next_node = curr.next # Store next node curr.next = prev # Reverse the link prev = curr # Move prev forward curr = next_node # Move curr forward return prev # New head
Problem: Reverse a singly linked list recursively. Input: 1 -> 2 -> 3 -> None Output: 3 -> 2 -> 1 -> None
1 -> 2 -> 3 -> None
3 -> 2 -> 1 -> None
head.next
new_head = reverseList(2 -> 3 -> None)
3 -> 2 -> None
1
2.next.next = 1
2 -> 1
3
def reverseList(head: ListNode) -> ListNode: if not head or not head.next: return head new_head = reverseList(head.next) head.next.next = head # Reverse the link head.next = None # Avoid cycle return new_head
Problem: Reverse a linked list from position m to n (1-indexed). Input: 1 -> 2 -> 3 -> 4 -> 5 -> None, m = 2, n = 4 Output: 1 -> 4 -> 3 -> 2 -> 5 -> None
m
n
m = 2
n = 4
1 -> 4 -> 3 -> 2 -> 5 -> None
(m-1)
curr = prev.next
n - m + 1
def reverseBetween(head: ListNode, m: int, n: int) -> ListNode: dummy = ListNode(0, head) prev = dummy # Move prev to (m-1)th node for _ in range(m - 1): prev = prev.next # Reverse sublist curr = prev.next for _ in range(n - m): next_node = curr.next curr.next = next_node.next next_node.next = prev.next prev.next = next_node return dummy.next
curr.next
m-1
if not head: return None
slow
fast
"Alright, let’s lock this in. To reverse a linked list: 1. Iterative: Use prev, curr, and next to flip links in one pass. Time: O(n), Space: O(1). 2. Recursive: Base case is head or head.next is None. Reverse the rest, then fix the current node’s link. Time: O(n), Space: O(n). 3. Sublist: Use a dummy node, traverse to m-1, reverse n-m+1 nodes, and reconnect. Common pitfalls? Losing next, cycles, and off-by-one errors. Practice the edge cases—empty list, single node, and full reversal. You’ve got this!
n-m+1
Now go ace that interview! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.