Two Pointers
Single Linked List
61. Rotate List
Medium·
1
Solution
O(2n)
O(1)
FIG. ROTATE LIST● INTERACTIVE
visualization loads as you reach it
- Time
- O(2n)
getLen(head)walks the whole list once:O(n), wherenis the number of nodes.- The
for _ in range(k)loop advancesrightat mostntimes (sincekis reduced modn), then thewhile right and right.nextloop advancesleft/righttogether at mostnmore times:O(n). - Total across both passes:
O(2n). - Space
- O(1)
- Only
sentinel,left,right, andkare tracked; no structure grows withn.
Kth from End of Linked List
Easy·
1
Solution
O(n)
O(1)
FIG. KTH FROM END OF LINKED LIST● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
fastadvancesksteps, thenfastandslowadvance together untilfastruns off the list - together they touch each node at most once,O(n).- Space
- O(1)
- Only the
slowandfastpointers are tracked.
Find the Sum of Last N nodes of the Linked List
Easy·
1
Solution
O(L + n)
O(1)
FIG. FIND THE SUM OF LAST N NODES OF THE LINK● INTERACTIVE
visualization loads as you reach it
- Time
- O(L + n)
Lis the total number of nodes in the list. Thefor _ in range(n)loop advancesfastbynnodes.- The
while fastloop then advances bothfastandslowtogether for the remainingL - nnodes untilfastruns off the list. - The final
while slowloop sums the lastnnodes. - These three passes add up to
n + (L - n) + n = L + n. - Space
- O(1)
- Only the pointers
slow,fastand the scalartotalare tracked, independent ofLorn.
19. Remove Nth Node From End of List
Medium·
2 Approachesclick to switch
1
Two Pass
O(2n)
O(1)
2
One Pass
O(n)
O(1)
FIG. REMOVE NTH NODE FROM END OF LIST● INTERACTIVE
visualization loads as you reach it
- Time
- O(2n)
- One
O(n)pass walkscurrto the end to countlength, then a second pass walkscurrfromsentinelup tooffsetsteps (at mostn) -2n. - Space
- O(1)
- Only
sentinel,length,offset, andcurrare tracked, no extra structure sized by the list.
369. Plus One Linked List
Medium·
2 Approachesclick to switch
1
Iterative
O(2n)
O(1)
2
Recursive
O(n)
O(n)
FIG. PLUS ONE LINKED LIST● INTERACTIVE
visualization loads as you reach it
- Time
- O(2n)
n= number of nodes inhead. The firstwhile currscan walks every node once to findrightmost_non9-O(n). The secondwhile currscan zeroes out every node after it - alsoO(n)in the worst case (all nines). Two separate passes give2n.- Space
- O(1)
- Only the
sentinel,curr, andrightmost_non9pointers are kept; nothing scales withn.
82. Remove Duplicates from Sorted List II
Medium·
1
Sentinel + Predecessor
O(n)
O(1)
FIG. REMOVE DUPLICATES FROM SORTED LIST II● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
curronly ever moves forward (including inside the innerwhilethat skips a duplicate run), visiting each of thennodes exactly once.- Space
- O(1)
- Only
sentinel,prev, andcurrare tracked, regardless ofn.
Slow & Fast Pointers
876. Middle of the Linked List
Easy·
3 Approachesclick to switch
1
Array conversion
O(n)
O(n)
2
Two Pass
O(2n)
O(1)
3
One Pass: Slow & Fast
O(n)
O(1)
FIG. MIDDLE OF THE LINKED LIST 2● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
- The
while headloop walks every one of thennodes once to fillarr. - Space
- O(n)
arrstores a reference to allnnodes.
2095. Delete the Middle Node of a Linked List
Medium·
3 Approachesclick to switch
1
Array conversion
O(n)
O(n)
2
Two Pass
O(2n)
O(1)
3
One Pass: Slow & Fast
O(n)
O(1)
FIG. DELETE THE MIDDLE NODE OF A LINKED LIST 2● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
- The
while currloop walks every node once to fillarr. - Space
- O(n)
arrstores a reference to every one of thennodes.
Insert in Middle of Linked List
Basic·
1
One Pass: Slow & Fast
O(n)
O(1)
FIG. INSERT IN MIDDLE OF LINKED LIST● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
slowandfastwalk the list once -fastadvances two steps per iteration, so the loop runs at mostn / 2times, stillO(n)fornnodes.- Space
- O(1)
- Only
new_node,slow, andfastare allocated; the list is spliced in place.
141. Linked List Cycle
Easy·
2 Approachesclick to switch
1
Hash Set
O(n)
O(n)
2
Floyd's Cycle Finding
O(n)
O(1)
FIG. LINKED LIST CYCLE 2● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
currvisits each of thennodes at most once before either finding a repeat or reaching the end.- Space
- O(n)
seencan grow to hold allnnodes when there is no cycle.
142. Linked List Cycle II
Medium·
2 Approachesclick to switch
1
Hash Set
O(n)
O(n)
2
Floyd's Tortoise and Hare
O(n)
O(1)
FIG. LINKED LIST CYCLE II 2● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
currwalks each of thennodes at most once, checking membership inseen.- Space
- O(n)
seencan grow to hold allnnodes if there is no cycle.
Find length of Loop
Easy·
2 Approachesclick to switch
1
Hash Set
O(2n)
O(n)
2
Floyd's Tortoise and Hare
O(2n)
O(1)
FIG. FIND LENGTH OF LOOP 2● INTERACTIVE
visualization loads as you reach it
- Time
- O(2n)
- The first
while currloop walks the list until it revisits a node, at mostnnodes -O(n). - Once a repeat is found, the inner
while slow != currloop walks back around the cycle to count its length, at mostnnodes - a secondO(n)pass. - Space
- O(n)
seenholds every node visited before a repeat is found, up to allnnodes in the worst case.
Remove loop in Linked List
Medium·
2 Approachesclick to switch
1
Hash Set
O(n)
O(n)
2
Floyd's Tortoise and Hare
O(n)
O(1)
FIG. REMOVE LOOP IN LINKED LIST 2● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
- The
while currloop visits each node exactly once viacurr = curr.next, doing anO(1)set lookup/insert (curr in seen,seen.add(curr)) per node:O(n), wherenis the number of nodes. - Space
- O(n)
seenstores a reference to every visited node before the loop is found (or the list ends), up toO(n).
Two Linked Lists
Identical Linked Lists
Basic·
1
Solution
O(n)
O(1)
FIG. IDENTICAL LINKED LISTS● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
nis the length of the shorter list - thewhile a and bloop advancesaandbtogether one node at a time, stopping as soon as either list runs out.- Space
- O(1)
- Only the pointers
aandbare tracked, regardless of list length.
2. Add Two Numbers
Medium·
2 Approachesclick to switch
1
In Place
O(m + n)
O(1)
2
New LL
O(m + n)
O(m + n)
FIG. ADD TWO NUMBERS 2● INTERACTIVE
visualization loads as you reach it
- Time
- O(m + n)
- The first
while x and yloop advances both lists together formin(m, n)steps, wherem = len(l1)andn = len(l2); whichever list is longer is then walked the rest of the way in the followingwhile x/while yloop - together every node of both lists is visited exactly once,m + n. - Space
- O(1)
- The digits are overwritten in place on the existing nodes (
x.val = y.val = rem), and only a constant number of scalars (carry,x_prev,y_prev) plus at most one extraListNode(1)for a final carry are allocated.
21. Merge Two Sorted Lists
Easy·
2 Approachesclick to switch
1
Iterative
O(m + n)
O(1)
2
Recursive
O(m + n)
O(m + n)
FIG. MERGE TWO SORTED LISTS● INTERACTIVE
visualization loads as you reach it
- Time
- O(m + n)
m = len(list1),n = len(list2). Thewhile a and bloop advances one pointer per iteration, consuming exactly one node fromlist1orlist2each time - at mostm + niterations total.- Space
- O(1)
- Only
sentinel,a,b, andcurrare tracked; existing nodes are relinked in place, no new nodes or structures are allocated.
1634. Add Two Polynomials Represented as Linked Lists
Medium·
1
Solution
O(m + n)
O(m + n)
FIG. ADD TWO POLYNOMIALS REPRESENTED AS LINKE● INTERACTIVE
visualization loads as you reach it
- Time
- O(m + n)
mandnare the lengths ofpoly1andpoly2.- The first
while a and bloop advancesa,b, or both by one node per iteration; the two trailingwhile a/while bloops drain whatever remains. Together every node of both lists is visited exactly once -m + ntotal steps. - Space
- O(m + n)
- Each step allocates a fresh
PolyNodeonto the result list viacurr.next = PolyNode(...); in the worst case (no matching powers cancel to zero) that is up tom + nnew nodes.
Go Circular
160. Intersection of Two Linked Lists
Easy·
4 Approachesclick to switch
1
Brute Force
O(m * n)
O(1)
2
Hash Set
O(m + n)
O(m)
3
Length Difference
O(2m + 2n)
O(1)
4
Two Pointers
O(m + n)
O(1)
FIG. INTERSECTION OF TWO LINKED LISTS 2● INTERACTIVE
visualization loads as you reach it
- Time
- O(m * n)
m = len(headA),n = len(headB). For every node ofheadAthe innerwhile ywalks the whole ofheadBlooking for a match -m * nin the worst case (no intersection).- Space
- O(1)
- Only the two traversal pointers
headAandyare kept; no extra structure grows with input size.
Circular Linked Lists
708. Insert into a Sorted Circular Linked List
Medium·
1
Solution
O(n)
O(1)
FIG. INSERT INTO A SORTED CIRCULAR LINKED LIS● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
nis the number of nodes in the circular list.prev/currwalk around the list at most once (the loop breaks whenprev == headagain), so the search is a singleO(n)pass.- Space
- O(1)
- Only
new_node,prev, andcurrare allocated/tracked; no structure scales withn.