PrepStack · Markdown HTML

Linked-list pointer rewiring

Pointers and sliding window · HTML

Formal shape

Before changing cur.next, save nxt=cur.next. After rewiring, update the boundaries of processed and unprocessed chains. A dummy turns head operations into ordinary successor operations.

Recognition signals

False friends

SignalWhy it is different
Read-only list traversalIf values are only inspected, no rewiring is needed; a simple or fast-slow traversal suffices.
Frequent index accessLinked storage is poor for random locations; conversion to an array may fit better.

Core invariant

prev bounds the correctly processed chain and cur is the first unprocessed node; every original node belongs to exactly one side and remains reachable.

State, transition, and processing order

ItemDefinition
StateOptional dummy, prev, cur, nxt, and operation-specific segment heads or tails.
TransitionSave nxt, redirect cur.next to its new destination, then advance prev=cur and cur=nxt.
Frontier / orderThe single boundary between a rewired processed prefix and an untouched original suffix.

Python skeleton

def reverse_list(head):
    prev, cur = None, head
    while cur is not None:
        nxt = cur.next
        cur.next = prev
        prev = cur
        cur = nxt
    return prev

Correctness sketch

  1. Saving nxt preserves reachability of the unprocessed suffix before cur.next is overwritten.
  2. Each round moves cur from the original suffix to the front of the correctly reversed prefix, preserving the partition.
  3. When cur is None, every node is in the reversed prefix, prev is its head, and every next edge has the desired direction.

Complexity

TimeSpaceParameters
O(n)O(1)n is the number of processed nodes; each next pointer changes a constant number of times.

Edge cases

Error patterns

PatternWhy it fails / fix
Overwriting next before saving itThe unprocessed suffix becomes permanently lost.
Returning the old headAfter full reversal, the new head is prev.
Not reconnecting a reversed segmentLocal reversal requires both the predecessor before the segment and successor after it.

Selection and exclusion rules

RelationTemplateBoundary
role-pointer simulationExplicit simulation / state machineSeveral pointer roles change by phase.
sequential-storage counterpartSame-direction / read-write pointersAn array write pointer becomes a predecessor and next link.

Original micro-example

PartDetail
ResultReturn c to obtain c→b→a→None.
SetupList a→b→c→None.
TracePoint a to None, b to a, and c to b, saving the old successor before every rewrite.

Recall prompts

My notes