sagelearnai

Linked Lists

Signal: the input is a ListNode chain; reversing, merging, reordering, detecting cycles, finding the middle or the k-th from the end; or O(1) insert/delete in the middle (LRU caches).

Core idea: linked-list problems are pointer surgery. Three tools cover almost everything:

  1. Dummy (sentinel) head. Removes special cases when the head itself may change.
  2. Iterative reversal with prev, curr, next.
  3. Fast & slow pointers. Fast moves 2 steps and slow moves 1. They find the middle, detect cycles, and find the k-th node from the end (with a k-step head start).
python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val, self.next = val, next

# Reverse in place
prev, curr = None, head
while curr:
    nxt = curr.next          # 1. save
    curr.next = prev         # 2. reverse the pointer
    prev, curr = curr, nxt   # 3. advance
return prev                  # new head

# Dummy head when building or deleting
dummy = tail = ListNode()
...
tail.next = node; tail = node
return dummy.next
Draw it

In interviews, draw 3–4 boxes and arrows, and update them step by step for your loop. It's the fastest way to avoid losing a pointer. The order of the three assignments in reversal matters.

In this course's runner, list inputs like [1, 2, 3] are converted to ListNode chains for you, and returned lists are converted back to arrays.

Problems

  1. Reverse Linked List (Easy): prev / curr / next pointer reversal
  2. Merge Two Sorted Lists (Easy): Dummy head + splice the smaller node
  3. Linked List Cycle (Easy): Floyd's tortoise and hare
  4. Remove Duplicates from Sorted List (Easy): Skip next while equal
  5. Intersection of Two Linked Lists (Easy): Switch heads at the end; pointers meet at the intersection
  6. Remove Nth Node From End of List (Medium): Lead pointer n steps ahead + dummy head
  7. Swap Nodes in Pairs (Medium): Dummy head; rewire each pair (prev → second → first → rest)
  8. Add Two Numbers (Medium): Digit-by-digit addition with carry (digits stored in reverse)
  9. Reorder List (Medium): Find middle → reverse second half → interleave
  10. Reverse Linked List II (Medium): Walk to node before `left`, then head-insert the sublist nodes
  11. Rotate List (Medium): Make it a ring, then cut at length − k % length
  12. Copy List with Random Pointer (Medium): Hash map old → new (or interleave copies)
  13. LRU Cache (Medium): Hash map + doubly linked list (or ordered map)
  14. Reverse Nodes in k-Group (Hard): Check k nodes exist; reverse the group; stitch; repeat
  15. Merge k Sorted Lists (Hard): Min-heap of the k current heads
  16. Remove Linked List Elements (Easy): Dummy head; unlink matching nodes
  17. Odd Even Linked List (Medium): Build odd and even chains, then append even after odd
  18. Partition List (Medium): Two dummy lists (< x and ≥ x), then join
  19. Sort List (Medium): Merge sort: split with fast/slow, sort halves, merge
  20. Design Linked List (Medium): Sentinel head + size counter; walk to index − 1
  21. Flatten a Multilevel Doubly Linked List (Medium): When a node has a child, splice the child list in before next (DFS)