Web Analytics

🚀 Advanced Linked List Problems

Advanced ~30 min read ⭐⭐⭐ FAANG Level

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

Output
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

  1. Two Pointers: Merge, partition
  2. Slow-Fast: Find middle for reorder
  3. Reversal: Reorder, rotate
  4. Dummy Nodes: Merge, partition, add numbers
  5. 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

  1. Combine techniques: Use multiple patterns together
  2. Dummy nodes are powerful: Simplify many problems
  3. Break complex problems: Into manageable steps
  4. Most are O(1) space: In-place solutions preferred
  5. 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! 🚀

Next Module: Continue your DSA journey with more advanced data structures and algorithms!