Linked List Patterns
Expert Answer & Key Takeaways
Linked List Fundamentals & Patterns
1. Types of Linked Lists
| Type | Description | Pointers |
|---|---|---|
| Singly | Forward traversal only. | next |
| Doubly | Forward and backward traversal. | next, prev |
| Circular | Last node points back to head. | next (tail -> head) |
2. Core Techniques
- Dummy Node: A sentinel node placed at the start to avoid edge cases (e.g., deleting head).
- Fast & Slow Pointers: Used for cycle detection, finding middle, and k-th node from end.
- Two Pointers: Used for merging, intersections, and partitioning.
- In-place Reversal: Changing
nextpointers without using extra memory.
3. Comparison with Arrays
| Feature | Linked List | Array |
|---|---|---|
| Access | (Sequential) | (Random) |
| Insert/Delete | (if at known node) | (Shift required) |
| Memory | Dynamic (Extra for pointers) | Static/Dynamic (Contiguous) |
4. Visualizing Reverse
Initial: [1] -> [2] -> [3] -> null
1. Point [1].next to null
2. Point [2].next to [1]
3. Point [3].next to [2]
Result: [3] -> [2] -> [1] -> nullLinked List Cycle
head, the head of a linked list, determine if the linked list has a cycle in it.List: 3 -> 2 -> 0 -> -4 -> 2... (Cycle)
1. Slow=3, Fast=2
2. Slow=2, Fast=-4
3. Slow=0, Fast=0 (MEET!)
Result: trueExamples
Level I: HashSet / Visited Set
Intuition
Visual Trace
List: 3 -> 2 -> 0 -> -4 (points back to 2)
Iter 1: node=3, Set={3}
Iter 2: node=2, Set={3, 2}
Iter 3: node=0, Set={3, 2, 0}
Iter 4: node=-4, Set={3, 2, 0, -4}
Iter 5: node=2, Found in Set! Cycle detected.- Initialize an empty HashSet of node references.
- Traverse the list. For each node, check if it exists in the set.
- If yes, return true. Otherwise, add the node to the set and move to next node.
Detailed Dry Run
3 -> 2 -> 0 -> -4 -> 2 (cycle)- Seen:
{3} - Seen:
{3, 2} - Seen:
{3, 2, 0} - Seen:
{3, 2, 0, -4} - Next is
2.2is in Seen. Cycle detected!
⚠️ Common Pitfalls & Tips
Level II: Node Marking
Intuition
next already points to this sentinel, we've closed the circle.Visual Trace
Nodes: [A] -> [B] -> [C] -> [B(cycle)]
1. At [A]: Save next [B], set [A].next = Sentinel. Move to [B].
2. At [B]: Save next [C], set [B].next = Sentinel. Move to [C].
3. At [C]: Save next [B], set [C].next = Sentinel. Move to [B].
4. At [B]: [B].next is already Sentinel! return true.Detailed Dry Run
⚠️ Common Pitfalls & Tips
Level III: Floyd's Cycle Finding (Tortoise and Hare)
Intuition
slow and fast. slow moves one step, fast moves two. If there's a cycle, the fast pointer will eventually 'lap' the slow pointer.Visual Trace
List: 1 -> 2 -> 3 -> 4 -> 5 -> (back to 2)
Start: S=1, F=1
Step 1: S=2, F=3
Step 2: S=3, F=5
Step 3: S=4, F=3 (F wrapped around cycle)
Step 4: S=5, F=5 (MEET!)Detailed Dry Run
1 -> 2 -> 1...- Initial:
slow=1, fast=2. - Step 1:
slow = slow.next (2), fast = fast.next.next (2). - Match! Return true.
⚠️ Common Pitfalls & Tips
fast and fast.next for null to avoid errors.Remove Nth Node From End of List
head of a linked list, remove the n^{th} node from the end of the list and return its head.Visual Representation
List: 1 -> 2 -> 3 -> 4 -> 5, n = 2
Step 1: Move Fast pointer n=2 steps ahead.
Dummy -> 1 -> 2 -> 3 -> 4 -> 5
S F
Step 2: Move both S and F until F reaches the end.
Dummy -> 1 -> 2 -> 3 -> 4 -> 5 -> null
S F
Step 3: S.next = S.next.next (Removes 4)
1 -> 2 -> 3 -> 5Examples
Level I: Two Pass (Length Calculation)
Intuition
Detailed Dry Run
1->2->3->4->5, n=2. Length = 5.
Target to skip: 5 - 2 = 3 steps from dummy.| Steps | Curr | Action |
|---|---|---|
| 0 | Dummy | Move |
| 1 | 1 | Move |
| 2 | 2 | Move |
| 3 | 3 | Stop. curr is now at node 3. curr.next (node 4) is the one to remove. |
curr.next = curr.next.next (node 3 points to node 5) |
⚠️ Common Pitfalls & Tips
Level II: Better (Recursion)
Intuition
n, we remove the next node.- Recursively call the function for
head.next. - On return, increment a counter.
- If counter equals
n, the current node'snextshould be skipped.
Detailed Dry Run
1->2->3, n=1- Push
1,2,3to stack. - Pop
3, count=1.count == n, so return to2acknowledging3should be removed? (Actually implementation differs slightly).
⚠️ Common Pitfalls & Tips
Level III: Optimal (One Pass - Two Pointers)
Intuition
fast and slow. Move fast n steps ahead. Then move both until fast reaches the end. slow will now be just before the node to be removed.- Initialize
fastandslowpointers at a dummy node. - Move the
fastpointer n+1 steps forward. - Move both pointers together until the
fastpointer reaches the end (null). - At this point,
slow.nextis the node to be removed. Updateslow.next = slow.next.next.
Detailed Dry Run
1->2->3->4->5, n=2- Start
fast,slowatdummy. - Move
fast3 steps:fastpoints at 3. - Move both:
fastat 4,slowat 1;fastat 5,slowat 2;fastatnull,slowat 3. slow.next(node 4) is removed.
⚠️ Common Pitfalls & Tips
Visual Representation
List: 1 -> 2 -> 3 -> 4 -> 5, n = 2
[D] -> [1] -> [2] -> [3] -> [4] -> [5] -> null
S,F
1. Move Fast 3 steps (n+1):
[D] -> [1] -> [2] -> [3] -> [4] -> [5]
S F
2. Move both until F is null:
[D] -> [1] -> [2] -> [3] -> [4] -> [5] -> null
S F
3. S.next = S.next.next (skip 4):
[3] -> [5]Partition List
head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions.Visual Representation
List: 1 -> 4 -> 3 -> 2 -> 5 -> 2, x = 3
Step 1: Create two lists: Small and Large.
Step 2: Traverse and assign:
Small: 1 -> 2 -> 2
Large: 4 -> 3 -> 5
Step 3: Connect Small.next to Large.next.
Result: 1 -> 2 -> 2 -> 4 -> 3 -> 5Examples
Level I: Brute Force (Extra Space)
Intuition
x. In the second pass, collect all nodes greater than or equal to x. Finally, rebuild the list.Visual Trace
List: 1 -> 4 -> 3 -> 2, x=3
Pass 1 (<3): [1, 2]
Pass 2 (>=3): [4, 3]
Combined: [1, 2, 4, 3]
New List: 1 -> 2 -> 4 -> 3Detailed Dry Run
[1, 4, 3, 2], x=3
Lists: Small=[1, 2], Large=[4, 3].
Rebuild: 1->2->4->3.⚠️ Common Pitfalls & Tips
Level II: In-place Node Movement
Intuition
Visual Trace
List: 1 -> 4 -> 3 -> 2, x=3
Small Head: [DummyS]
Large Head: [DummyL]
Node 1: <3, DummyS -> 1
Node 4: >=3, DummyL -> 4
Node 3: >=3, DummyL -> 4 -> 3
Node 2: <3, DummyS -> 1 -> 2
Connect: DummyS -> 1 -> 2 -> 4 -> 3Detailed Dry Run
⚠️ Common Pitfalls & Tips
Level III: Optimal (Two Pointer - Separate Lists)
Intuition
x and another for nodes greater than or equal to x. Finally, link the end of the first list to the head of the second list.Detailed Dry Run
1->4->3->2, x=3- 1 < 3: Small:
1 - 4 >= 3: Large:
4 - 3 >= 3: Large:
4->3 - 2 < 3: Small:
1->2 - End: Connect
1->2to4->3->1->2->4->3
⚠️ Common Pitfalls & Tips
after.next = null to avoid cycles in the list.Merge K Sorted Lists
k linked-lists lists, each linked-list is sorted in ascending order.Visual Representation
lists = [
1->4->5,
1->3->4,
2->6
]
Min-Heap State:
1. Insert heads: [1, 1, 2]
2. Extract 1, add next(4): Heap = [1, 2, 4]
3. Extract 1, add next(3): Heap = [2, 3, 4]
4. Extract 2, add next(6): Heap = [3, 4, 6]...
Merged Result:
1->1->2->3->4->4->5->6Examples
Level I: Brute Force (Array + Sort)
Intuition
Detailed Dry Run
- Collect: [1,4,5,1,3,4,2,6]
- Sort: [1,1,2,3,4,4,5,6]
- Build new list: 1->1->2->3->4->4->5->6
⚠️ Common Pitfalls & Tips
Visual Trace
Lists: [1->4, 1->3]
1. Array: [1, 4, 1, 3]
2. Sorted: [1, 1, 3, 4]
3. Linked: 1 -> 1 -> 3 -> 4Level II: Divide and Conquer
Intuition
Visual Trace
[L1, L2, L3, L4]
\ / \ /
[L12] [L34]
\ /
[Final]Detailed Dry Run
⚠️ Common Pitfalls & Tips
Level III: Min-Heap (Optimal)
Intuition
Detailed Dry Run
- Heap: [(1: from L1), (1: from L2), (2: from L3)]
- Pop 1 (L1). Res: [1]. Push 4 (L1). Heap: [1, 2, 4]
- Pop 1 (L2). Res: [1,1]. Push 3 (L2). Heap: [2, 3, 4]...
⚠️ Common Pitfalls & Tips
i or a wrapper is necessary for heapq when values are equal.Reverse Nodes in k-Group
List: 1 -> 2 -> 3 -> 4 -> 5, k = 3
1. Group [1,2,3] -> Reverse -> 3 -> 2 -> 1
2. Group [4,5] (Size 2 < 3) -> Keep 4 -> 5
Result: 3 -> 2 -> 1 -> 4 -> 5Examples
Level I: Recursive Approach
Intuition
Detailed Dry Run
- First 2 nodes: 1->2. Reverse: 2->1.
- Recursive call on 3->4->5.
- Link 1 to the result of recursive call.
⚠️ Common Pitfalls & Tips
Level II: Iterative with Stack
Intuition
Visual Trace
List: 1 -> 2 -> 3 -> 4, k=2
Iter 1: Stack=[1, 2]. Pop: 2 -> 1
Iter 2: Stack=[3, 4]. Pop: 4 -> 3
Result: 2 -> 1 -> 4 -> 3Detailed Dry Run
Level III: Optimal (Iterative Constant Space)
Intuition
Detailed Dry Run
- Reverse 1, 2: Dummy -> 2 -> 1 -> 3.
- Next group 3, 4: Dummy -> 2 -> 1 -> 4 -> 3 -> 5.
⚠️ Common Pitfalls & Tips
Merge Two Sorted Lists
Visual Representation
L1: 1 -> 2 -> 4
L2: 1 -> 3 -> 4
Result: 1 -> 1 -> 2 -> 3 -> 4 -> 4Examples
Level I: Iterative
Intuition
Visual Pointer Walk
L1: [1] -> [3]
L2: [2] -> [4]
1. Compare 1 and 2. Attach 1. Tail=[1], L1=[3]
2. Compare 3 and 2. Attach 2. Tail=[2], L2=[4]
3. Compare 3 and 4. Attach 3. Tail=[3], L1=null
4. Attach remaining L2: Tail points to [4].Detailed Dry Run
⚠️ Common Pitfalls & Tips
Level III: Optimized In-place (No Dummy)
Intuition
Detailed Dry Run
- L1.val (1) < L2.val (2). Result head is 1.
- Iterate and link nodes in-place.
⚠️ Common Pitfalls & Tips
Reverse Linked List
head of a singly linked list, reverse the list, and return the reversed list.Visual Representation
List: 1 -> 2 -> 3 -> null
Step 1: Current=1, Next=2. 1.next points to null.
Step 2: Current=2, Next=3. 2.next points to 1.
Step 3: Current=3, Next=null. 3.next points to 2.
Result: 3 -> 2 -> 1 -> nullExamples
Level I: Brute Force (Stack)
Intuition
Detailed Dry Run
- Push 1, 2, 3 to stack.
- Pop 3: New list 3.
- Pop 2: 3->2.
- Pop 1: 3->2->1.
⚠️ Common Pitfalls & Tips
Level II: Recursive
Intuition
next back to head.Detailed Dry Run
- reverseList(2->3).
- reverseList(3) returns 3.
- Back at 2: 2.next (3).next = 2. 2.next = null. List: 3->2->null.
- Back at 1: 1.next (2).next = 1. 1.next = null. List: 3->2->1->null.
⚠️ Common Pitfalls & Tips
head.next = null, causing deep cycles.Level III: Optimal (Iterative In-place)
Intuition
prev, curr, and next. Iterate through the list, changing each node's next pointer to point to the prev node.Detailed Dry Run
- Next=2. 1.next=null. Prev=1, Curr=2.
- Next=3. 2.next=1. Prev=2, Curr=3.
- Next=null. 3.next=2. Prev=3, Curr=null. Return Prev (3).
⚠️ Common Pitfalls & Tips
prev at the end, as curr will be null.Visual Pointer Reversal
[1] -> [2] -> [3] -> null
p=null, c=1
Iter 1: [1].next=null, p=1, c=2
Iter 2: [2].next=1, p=2, c=3
Iter 3: [3].next=2, p=3, c=null
Return p=3: [3] -> [2] -> [1] -> nullMiddle of the Linked List
head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node.Visual Representation
List: 1 -> 2 -> 3 -> 4 -> 5
Slow moves 1 step, Fast moves 2 steps.
Start: Slow=1, Fast=1
Step 1: Slow=2, Fast=3
Step 2: Slow=3, Fast=5
Result: 3 is middle node.Examples
Level I: Brute Force (Array)
Intuition
size / 2.Detailed Dry Run
⚠️ Common Pitfalls & Tips
Visual Mapping
List: A -> B -> C -> D
Array: [A, B, C, D]
Size: 4
Middle: size/2 = 2
Result: Array[2] = CLevel II: Two Pass (Length Calculation)
Intuition
Detailed Dry Run
- Pass 1: Count = 4.
- Target index = 4/2 = 2.
- Pass 2: Move to index 2 (node with value 3).
⚠️ Common Pitfalls & Tips
length/2 times to handle both even and odd lengths correctly.Level III: Optimal (Two Pointers - Fast & Slow)
Intuition
slow and fast. Move slow one step at a time and fast two steps at a time. When fast reaches the end, slow will be at the middle.Detailed Dry Run
- S=1, F=1.
- S=2, F=3.
- S=3, F=5. F.next is null, return S=3.
⚠️ Common Pitfalls & Tips
Reorder List
L0 → L1 → … → Ln - 1 → LnL0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …Visual Representation
List: 1 -> 2 -> 3 -> 4
1. Find middle: 2
2. Reverse second half: 4 -> 3
3. Merge lists: 1 -> 4 -> 2 -> 3Examples
Level I: Brute Force (Array/Stack)
Intuition
next pointers.Detailed Dry Run
- 1.next = 4.
- 4.next = 2.
- 2.next = 3.
- 3.next = null.
⚠️ Common Pitfalls & Tips
Visual Trace (Array/Stack)
List: 1 -> 2 -> 3 -> 4
Array: [1, 2, 3, 4]
i=0, j=3: 1.next=4, 4.next=2
i=1, j=2: 2.next=3
i=2, j=2: Break. 3.next=null
Result: 1 -> 4 -> 2 -> 3Level II: Using a Stack
Intuition
Detailed Dry Run
- Pop 4, link after 1: 1->4->2.
- Pop 3, link after 2: 1->4->2->3. Wait, stop after N/2 swaps.
⚠️ Common Pitfalls & Tips
next pointer to null to prevent cycles.Level III: Optimal (Middle + Reverse + Merge)
Intuition
Detailed Dry Run
- Mid: 2.
- Reverse 3->4 to 4->3.
- Merge (1->2) and (4->3): 1->4->2->3.
⚠️ Common Pitfalls & Tips
slow.next = null) to avoid cycles.Copy List with Random Pointer
Visual Representation
Original: [7,null] -> [13,0] -> [11,4] -> [10,2] -> [1,0]
Where [val, random_index]
Approach: Interweaving
1. Interweave: 7 -> 7' -> 13 -> 13' -> ...
2. Assign random: 7'.random = 7.random.next
3. Separate: 7 -> 13 and 7' -> 13'Examples
Level I: Using HashMap
Intuition
next and random pointers using the map.Detailed Dry Run
- 7'.next = Map[7.next] -> 13'.
- 7'.random = Map[7.random] -> null.
⚠️ Common Pitfalls & Tips
Node structure (with val, next, random) is understood.Visual Mapping
Original: [A] -> [B] -> [C]
Map: {A: A', B: B', C: C'}
Step 2: A'.next = Map[B], A'.random = Map[C]Level II: Recursive DFS with Map
Intuition
next and random pointers. This naturally handles cycles and arbitrary pointers.Detailed Dry Run
- 7'.next = copy(13).
- 7'.random = copy(null). Recursion handles the rest via map memoization.
⚠️ Common Pitfalls & Tips
Level III: Optimal (Interweaving Nodes)
Intuition
random pointers. Step 3: Separate the interweaved lists.Detailed Dry Run
- 7'.random = 7.random.next.
- Original: 7->13, Copy: 7'->13'.
⚠️ Common Pitfalls & Tips
Add Two Numbers
Visual Representation
(2 -> 4 -> 3) + (5 -> 6 -> 4) = 7 -> 0 -> 8
Explanation: 342 + 465 = 807.Examples
Level I: Iterative (Elementary Math)
Intuition
Detailed Dry Run
- 2+5=7, carry=0.
- 4+6=10, carry=1. Node 0.
- Final carry 1. Node 1. Result: 7->0->1.
⚠️ Common Pitfalls & Tips
Level II: Recursive
Intuition
Detailed Dry Run
- Add(2, 5, 0): val=7, next=Add(4, 6, 0).
- Add(4, 6, 0): val=0, next=Add(null, null, 1).
- Add(null, null, 1): val=1, next=null. Result: 7->0->1.
⚠️ Common Pitfalls & Tips
Level III: Optimized Iterative (In-place Re-use)
Intuition
- Determine which list is longer (or just pick one).
- Add values and carry to the existing nodes.
- If the list ends while carry > 0, append a new node.
Detailed Dry Run
- l2[0] = (2+5)%10 = 7. carry=0.
- l2[1] = (4+6)%10 = 0. carry=1.
- l2[2] = (1+7)%10 = 8. carry=0. Return l2.
⚠️ Common Pitfalls & Tips
Intersection of Two Linked Lists
headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.Visual Representation
List A: a1 -> a2
\
c1 -> c2 -> c3
/
List B: b1 -> b2 -> b3
Intersection at c1.Examples
Level I: Brute Force (Hashing)
Intuition
Detailed Dry Run
⚠️ Common Pitfalls & Tips
Level II: Length Difference
Intuition
Visual Trace
A: 1 -> 2 -> 3 -> C1 (Len 4)
B: 4 -> C1 (Len 2)
Diff = 2. Advance pointer A by 2 steps.
Pointer A starts at 3, B starts at 4.
Move both: A=C1, B=C1. Match!Detailed Dry Run
Level III: Optimal (Two Pointers)
Intuition
pA and pB. Traverse both lists. When a pointer reaches the end, redirect it to the head of the other list. They will meet at the intersection point or both become null.Detailed Dry Run
- pA=1, pB=3.
- pA=2, pB=c.
- pA=c, pB=null->pA=1.
- pA=null->pB=3, pB=c. They meet after switched paths.
⚠️ Common Pitfalls & Tips
Palindrome Linked List
head of a singly linked list, return true if it is a palindrome or false otherwise.Visual Representation
List: 1 -> 2 -> 2 -> 1
1. Find mid: 2 (first 2)
2. Reverse second half: 1 -> 2
3. Compare halves: (1,2) == (1,2) -> trueExamples
Level I: Brute Force (Array Copy)
Intuition
Detailed Dry Run
⚠️ Common Pitfalls & Tips
Visual Trace (Array)
List: 1 -> 2 -> 3 -> 2 -> 1
Array: [1, 2, 3, 2, 1]
i=0, j=4: 1==1
i=1, j=3: 2==2
Match!Level II: Recursive / Stack-based
Intuition
Visual Trace (Stack)
Stack: [1, 2, 2, 1]
Compare Pop(1) with Head(1) -> MatchLevel III: Optimal (Reverse Second Half)
Intuition
Detailed Dry Run
- Mid: 2.
- Reverse second half: 1->2.
- Compare (1->2) and (1->2).
⚠️ Common Pitfalls & Tips
slow pointer will be exactly at the middle node, and reversing from it works fine.Remove Duplicates from Sorted List
head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.Visual Representation
List: 1 -> 1 -> 2 -> 3 -> 3
1. 1 == 1. 1.next points to 2.
2. 2 != 1. Move to 2.
3. 3 == 3. 3.next points to null.
Result: 1 -> 2 -> 3Examples
Level I: Iterative
Intuition
Detailed Dry Run
- head=1, next=1. Val 1 == 1. 1.next = 1.next.next = 2.
- head=1, next=2. Val 1 != 2. head=2.
- head=2, next=null. End.
⚠️ Common Pitfalls & Tips
Level II: Recursive
Intuition
Detailed Dry Run
- head=1, next=1. Val 1 == 1. Return deleteDuplicates(1.next).
- head=1, next=2. Val 1 != 2. head.next = deleteDuplicates(2). Return head.
- head=2, next=null. Return head.
⚠️ Common Pitfalls & Tips
Level III: Iterative with Sentinel (Dummy Node)
Intuition
- Initialize a
dummynode pointing tohead. - Iterate through the list using a
currpointer. - Compare
currwithcurr.nextand skip duplicates.
Detailed Dry Run
- curr = 1. curr.next = 1. Match. curr.next = 2.
- curr = 1. curr.next = 2. No match. curr = 2.
- curr = 2. curr.next = null. End.
⚠️ Common Pitfalls & Tips
Linked List Cycle II
Visual Representation
3 -> 2 -> 0 -> -4
^ |
+----------+
Cycle starts at node with value 2.Examples
Level I: HashSet
Intuition
Level II: Node Marking (Magic Value)
Intuition
Detailed Dry Run
- 3.val = 100001.
- 2.val = 100001.
- 0.val = 100001.
- -4.val = 100001.
- Next is 2, whose val is 100001. Cycle start found.
⚠️ Common Pitfalls & Tips
Level III: Floyd's Cycle-Finding Algorithm
Intuition
Visual Trace
D = Distance to cycle, S = Meeting point from start, C = Cycle length.
Moving D steps from head and D steps from meeting point hits cycle start.Remove Linked List Elements
Node.val == val.Visual Representation
List: 1 -> 6 -> 2, val = 6
Result: 1 -> 2Examples
Level I: Iterative
Intuition
val.Level II: Recursive
Intuition
Detailed Dry Run
- head=1, next=remove(6->2, 6).
- head=6, next=remove(2, 6).
- head=2, next=remove(null, 6) returns null.
- 2.val != 6, returns 2.
- 6.val == 6, returns remove(2, 6) which is 2.
- 1.next = 2, returns 1.
Odd Even Linked List
Visual Representation
1 -> 2 -> 3 -> 4 -> 5
Odd: 1 -> 3 -> 5
Even: 2 -> 4
Result: 1 -> 3 -> 5 -> 2 -> 4Examples
Level I: Brute Force (Two Lists/Arrays)
Intuition
Detailed Dry Run
Level III: Two Pointers (In-place)
Intuition
Swap Nodes in Pairs
Visual Representation
1 -> 2 -> 3 -> 4
Result: 2 -> 1 -> 4 -> 3Examples
Level I: Recursive
Intuition
Level III: Optimal (Iterative)
Intuition
prev pointer to track the node before the current pair being swapped.Detailed Dry Run
- First = 1, Second = 2.
- Prev.next = 2.
- 1.next = 3.
- 2.next = 1. Prev = 1.
Rotate List
Visual Representation
1 -> 2 -> 3 -> 4 -> 5, k = 2
Output: 4 -> 5 -> 1 -> 2 -> 3Level III: Cycle + Break
Intuition
Level II: Two Pointers
Intuition
first and second. Move first k steps ahead. Then move both until first reaches the end. second will then be at the node before the new head.Detailed Dry Run
- First moves 2 steps to 3.
- Move both until First is at 5. Second is at 3.
- New head = 3.next = 4.
- 3.next = null, 5.next = head.
Convert Sorted List to BST
Visual Representation
List: -10 -> -3 -> 0 -> 5 -> 9
Root is 0.Level I: Brute Force (Convert to Array)
Intuition
Detailed Dry Run
- Root = 0.
- Left = [-10, -3].
- Right = [5, 9].
Level III: Recursive (Middle find)
Intuition
Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.