Three Pointers
206. Reverse Linked List
Easy·
2 Approachesclick to switch
1
Iterative
O(n)
O(1)
2
Recursive
O(n)
O(n)
FIG. REVERSE LINKED LIST● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
frontwalks the list once,nbeing the number of nodes.- Space
- O(1)
- Only
front,mid, andbackare kept, regardless of list length.
Reverse a Doubly Linked List
Easy·
2 Approachesclick to switch
1
Iterative
O(n)
O(1)
2
Recursive
O(n)
O(n)
FIG. REVERSE A DOUBLY LINKED LIST● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
frontwalks forward through each of thennodes exactly once.- Space
- O(1)
- Only
front,mid, andbackare tracked, regardless ofn.
Reverse both parts
Easy·
1
Iterative
O(n)
O(1)
FIG. REVERSE BOTH PARTS● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
frontwalks every one of thennodes in the list exactly once, rewiring.nextpointers in place.- Space
- O(1)
- Only a fixed set of pointer variables (
front,mid,back,new_head,sep,tail) are tracked, independent of the list's length.
24. Swap Nodes in Pairs
Medium·
3 Approachesclick to switch
1
Iterative
O(n)
O(1)
2
Recursive
O(n)
O(n)
3
⛔ Swap Values
O(n)
O(1)
FIG. SWAP NODES IN PAIRS● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
nis the number of nodes - thewhile frontloop advancesback,mid, andfronttwo nodes at a time, visiting each node once.- Space
- O(1)
- Only the
sentinelnode and the three pointersfront,mid,backare allocated, regardless of list length.