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!