SDE & MLE Prep

Linked List

Toolkit

  • Dummy head — removes "is this the head?" special cases when deleting/inserting.
  • Fast & slow pointers — middle of list, cycle detection (Floyd), n-th from end.
  • In-place reversal — prev, cur = None, head; while cur: cur.next, prev, cur = prev, cur, cur.next.
  • Many hard problems = combination of the three: find middle → reverse second half → merge/compare.
reverse:   prev   cur    nxt
           None   1 ->   2 -> 3
           None <-1      2 -> 3       (cur.next = prev; prev = cur; cur = nxt)

Locally, Python tests use make_list([..]) / list_vals(node); Java tests use ListNode.of(..) (prints like [1, 2, 3]).

Problems