๐ Advanced Linked List Problems
You've mastered the fundamentals. Now it's time to combine them! Advanced linked list problems require using multiple techniques together: reversal + two pointers, dummy nodes + partition, slow-fast + merge. These are the problems asked at Google, Amazon, Microsoft, and Facebook!
๐ฏ FAANG Interview Alert!
These 8 problems appear frequently:
- โ Merge Two Sorted Lists - Google, Amazon
- โ Reorder List - Facebook, Microsoft
- โ Add Two Numbers - Amazon, Apple
- โ Partition List - LinkedIn, Google
- โ Rotate List - Microsoft, Amazon
- โ Remove Duplicates - Common warm-up
- โ Clone with Random - Advanced challenge
- โ Flatten Multilevel - Recursion test
Master these = Ace FAANG interviews!
The 8 Essential Problems
See Them in Action
Watch how we combine techniques:
Problem 1: Merge Two Sorted Lists
The Problem
Merge two sorted linked lists into one sorted list.
Example:
List 1: 1 โ 3 โ 5
List 2: 2 โ 4 โ 6
Merged: 1 โ 2 โ 3 โ 4 โ 5 โ 6
Solution: Two Pointers
def merge_sorted(list1, list2):
dummy = Node(0)
current = dummy
while list1 and list2:
if list1.data <= list2.data:
current.next = list1
list1 = list1.next
else:
current.next = list2
list2 = list2.next
current = current.next
# Attach remaining
current.next = list1 if list1 else list2
return dummy.next
Time: O(m + n), Space: O(1)
Problem 2: Reorder List
The Problem
Reorder L0โL1โL2โ...โLn to L0โLnโL1โLn-1โL2โLn-2...
Example:
Input: 1 โ 2 โ 3 โ 4 โ 5
Output: 1 โ 5 โ 2 โ 4 โ 3
Solution: Find Middle + Reverse + Merge
def reorder_list(head):
# 1. Find middle (slow-fast)
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# 2. Reverse second half
prev = None
current = slow
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
# 3. Merge alternating
first = head
second = prev
while second.next:
first_next = first.next
second_next = second.next
first.next = second
second.next = first_next
first = first_next
second = second_next
Time: O(n), Space: O(1)
Combines: Slow-fast + Reversal + Merge!
Problem 3: Add Two Numbers
The Problem
Add two numbers represented as linked lists (digits in reverse order).
Example:
342 + 465 = 807
Stored as: 2โ4โ3 + 5โ6โ4 = 7โ0โ8
Solution: Handle Carry
def add_two_numbers(l1, l2):
dummy = Node(0)
current = dummy
carry = 0
while l1 or l2 or carry:
val1 = l1.data if l1 else 0
val2 = l2.data if l2 else 0
total = val1 + val2 + carry
carry = total // 10
digit = total % 10
current.next = Node(digit)
current = current.next
if l1: l1 = l1.next
if l2: l2 = l2.next
return dummy.next
Time: O(max(m, n)), Space: O(max(m, n))
Problem 4: Partition List
The Problem
Partition list so all nodes < x come before nodes โฅ x.
Example:
Input: 3 โ 5 โ 8 โ 5 โ 10 โ 2 โ 1, x = 5
Output: 3 โ 2 โ 1 โ 5 โ 8 โ 5 โ 10
Solution: Two Dummy Nodes
def partition(head, x):
less_dummy = Node(0)
greater_dummy = Node(0)
less = less_dummy
greater = greater_dummy
while head:
if head.data < x:
less.next = head
less = less.next
else:
greater.next = head
greater = greater.next
head = head.next
greater.next = None
less.next = greater_dummy.next
return less_dummy.next
Time: O(n), Space: O(1)
Key: Dummy nodes simplify edge cases!
Problem 5: Rotate List
The Problem
Rotate list to the right by k places.
Example:
Input: 1 โ 2 โ 3 โ 4 โ 5, k = 2
Output: 4 โ 5 โ 1 โ 2 โ 3
Solution: Find New Head/Tail
def rotate_right(head, k):
# Get length and tail
length = 1
tail = head
while tail.next:
length += 1
tail = tail.next
# Optimize k
k = k % length
if k == 0:
return head
# Find new tail
new_tail = head
for _ in range(length - k - 1):
new_tail = new_tail.next
# Rotate
new_head = new_tail.next
new_tail.next = None
tail.next = head
return new_head
Time: O(n), Space: O(1)
The Complete Code
Click Run to execute your code
Complexity Summary
| Problem | Time | Space | Technique |
|---|---|---|---|
| Merge sorted lists | O(m+n) | O(1) โ | Two pointers |
| Reorder list | O(n) | O(1) โ | Slow-fast + Reverse |
| Add two numbers | O(max(m,n)) | O(max(m,n)) | Carry handling |
| Partition list | O(n) | O(1) โ | Dummy nodes |
| Rotate list | O(n) | O(1) โ | Find new head |
| Remove duplicates | O(n) | O(1) โ | Single pass |
| Clone with random | O(n) | O(1) โ | Interweaving |
| Flatten multilevel | O(n) | O(1) โ | Recursion/Stack |
Common Patterns
Pattern Recognition
- Two Pointers: Merge, partition
- Slow-Fast: Find middle for reorder
- Reversal: Reorder, rotate
- Dummy Nodes: Merge, partition, add numbers
- Multiple Passes: Often clearer than single pass
Interview Tips
๐ก How to Ace Advanced Problems
- โ Break into steps: Don't try to do everything at once
- โ Use dummy nodes: Simplify edge cases
- โ Draw it out: Visualize transformations
- โ Combine techniques: Slow-fast + reverse + merge
- โ Check edge cases: Empty, single node, even/odd
- โ Explain your approach: Show you understand WHY
Summary
Advanced Problems Mastery
- Combine techniques: Use multiple patterns together
- Dummy nodes are powerful: Simplify many problems
- Break complex problems: Into manageable steps
- Most are O(1) space: In-place solutions preferred
- Practice these 8: They appear in FAANG interviews!
Interview tip: Advanced problems test your ability to combine basic techniques. Draw the transformations, explain your approach step-by-step, and always check edge cases!
Congratulations!
๐ You've Completed Module 5: Linked Lists!
What you've mastered:
- โ Singly linked lists - basics and operations
- โ Reversal - 3-pointer technique
- โ Cycle detection - Floyd's algorithm
- โ Two pointers - slow-fast and gap patterns
- โ Doubly linked lists - bidirectional power
- โ Circular linked lists - no end!
- โ Advanced problems - combining everything!
You're now ready for FAANG linked list interviews! ๐
Enjoying these tutorials?