Bird
Raised Fist0
Interview Prepfast-slow-pointersmediumGoogleAmazon

Split Linked List in Parts

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Steps
setup

Initialize total_nodes and current pointer

Set total_nodes to 0 and current pointer to head of the list to start counting nodes.

💡 Counting nodes is essential to determine how to split the list evenly.
Line:total_nodes = 0 current = head
💡 We start with no nodes counted and a pointer at the list head.
📊
Split Linked List in Parts - Watch the Algorithm Execute, Step by Step
Watching each pointer move and link break helps you understand how the list is divided evenly and why some parts may be empty.
Step 1/18
·Active fillAnswer cell
advance
1
2
3
advance
1
2
3
Result: 1
advance
1
2
3
Result: 2
advance
1
2
3
Result: 3
advance
1
2
3
Result:
Part size:0
Remainder:3
advance
1
2
3
Result:
Parts:[null, null, null, null, null]
connect
1
2
3
Result:
Parts:[[1], null, null, null, null]
Remainder:2
advance
1
2
3
Result:
Parts:[[1], null, null, null, null]
detach
1
2
3
Result:
Parts:[[1], null, null, null, null]
connect
1
2
3
Result:
Parts:[[1], [2], null, null, null]
Remainder:1
advance
1
2
3
Result:
Parts:[[1], [2], null, null, null]
detach
1
2
3
Result:
Parts:[[1], [2], null, null, null]
connect
1
2
3
Result:
Parts:[[1], [2], [3], null, null]
Remainder:0
advance
1
2
3
Result:
Parts:[[1], [2], [3], null, null]
detach
1
2
3
Result:
Parts:[[1], [2], [3], null, null]
connect
1
2
3
Result:
Parts:[[1], [2], [3], null, null]
connect
1
2
3
Result:
Parts:[[1], [2], [3], null, null]
reconstruct
1
2
3
Result: [[1], [2], [3], null, null]

Key Takeaways

The algorithm counts nodes first to determine exact part sizes and remainder for even distribution.

This counting step is crucial and often overlooked; without it, splitting evenly is impossible.

Parts with remainder get one extra node, ensuring the first few parts are larger if nodes don't divide evenly.

Visualizing remainder distribution clarifies why some parts have one node and others are empty.

Breaking links inline isolates parts without extra memory, showing efficient in-place list manipulation.

Seeing links broken step-by-step reveals how the list is physically split, which is hard to grasp from code alone.

Practice

(1/5)
1. Given the following recursive code to convert a binary number in a linked list to an integer, what is the final output when the linked list is 1 -> 0 -> 1?
easy
A. 3
B. 7
C. 6
D. 5

Solution

  1. Step 1: Trace helper with node1 (val=1), acc=0

    acc = (0 << 1) | 1 = 1; recurse with node2
  2. Step 2: Trace helper with node2 (val=0), acc=1

    acc = (1 << 1) | 0 = 2; recurse with node3
  3. Step 3: Trace helper with node3 (val=1), acc=2

    acc = (2 << 1) | 1 = 5; recurse with None
  4. Step 4: Trace helper with None, acc=5

    Return acc=5
  5. Final Answer:

    Option D -> Option D
  6. Quick Check:

    Binary 101 equals decimal 5 [OK]
Hint: Shift and OR accumulate bits from left to right [OK]
Common Mistakes:
  • Off-by-one errors in shifting
  • Confusing bitwise OR with addition
  • Returning intermediate instead of final accumulated value
2. You are given a singly linked list where each node contains an additional pointer that can point to any node in the list or null. The task is to create a deep copy of this list, preserving both the next and random pointer structure. Which approach guarantees an optimal solution with O(n) time and O(1) extra space?
easy
A. Interleave copied nodes within the original list, assign random pointers using the interleaved structure, then separate the lists.
B. Use a hash map to store original-to-copy node mappings and create the copy in two passes.
C. Use a recursive function with memoization to copy nodes and their random pointers.
D. Traverse the list multiple times, copying nodes and random pointers separately without extra data structures.

Solution

  1. Step 1: Understand the problem constraints

    The problem requires copying a complex linked list with random pointers efficiently.
  2. Step 2: Identify the optimal approach

    Interleaving copied nodes within the original list allows assigning random pointers without extra space, then separating the lists restores the original and creates the copy.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Interleaving nodes achieves O(n) time and O(1) space [OK]
Hint: Interleaving nodes avoids extra space for mapping [OK]
Common Mistakes:
  • Assuming recursion is always optimal
  • Using extra hash maps increases space complexity
  • Trying multiple traversals without mapping fails random pointer assignment
3. Given the following code for partitioning a linked list around value x = 3, and the input list: 1 -> 4 -> 2 -> 5, what is the final reordered list returned by the function?
easy
A. 1 -> 4 -> 2 -> 5
B. 1 -> 2 -> 4 -> 5
C. 2 -> 1 -> 4 -> 5
D. 4 -> 1 -> 2 -> 5

Solution

  1. Step 1: Trace initial pointers with input 1 -> 4 -> 2 -> 5 and x=3

    Start with dummy -> 1 -> 4 -> 2 -> 5, less_tail and prev at dummy, current at 1.
  2. Step 2: Process nodes

    Node 1 < 3, less_tail.next == current, move less_tail and prev forward. Node 4 >= 3, move prev and current forward. Node 2 < 3, less_tail.next != current, so remove 2 and insert after less_tail. Final list: 1 -> 2 -> 4 -> 5.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Nodes less than 3 appear first in original order, then others [OK]
Hint: Track less_tail and prev carefully to avoid skipping nodes [OK]
Common Mistakes:
  • Swapping values instead of nodes
  • Incorrect pointer updates causing lost nodes
  • Assuming order changes within partitions
4. Consider the following Python code implementing the recursive reorder list approach. Given the input list 1->2->3, what is the printed output after calling reorderList(n1)?
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def reorderList(head):
    def helper(front, back):
        if not back:
            return True
        if not helper(front, back.next):
            return False
        if front[0] == back or front[0].next == back:
            back.next = None
            return False
        tmp = front[0].next
        front[0].next = back
        back.next = tmp
        front[0] = tmp
        return True
    helper([head], head)

n3 = ListNode(3)
n2 = ListNode(2, n3)
n1 = ListNode(1, n2)
reorderList(n1)
curr = n1
while curr:
    print(curr.val, end=' ')
    curr = curr.next
easy
A. 1 3
B. 1 2 3
C. 1 3 2
D. 3 2 1

Solution

  1. Step 1: Trace helper calls with input 1->2->3

    Recursion reaches back=null, then unwinds, linking nodes alternately: 1->3->2.
  2. Step 2: Verify final list traversal output

    Prints nodes in order: 1 3 2, confirming correct reorder.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Output matches reorder pattern for 3 nodes [OK]
Hint: Small input trace confirms reorder sequence [OK]
Common Mistakes:
  • Assuming no reorder happens
  • Stopping recursion too early
  • Misplacing next pointers
5. You are given a singly linked list and an integer k. The task is to reverse the nodes of the list k at a time and return the modified list. If the number of nodes is not a multiple of k, the remaining nodes at the end should remain as is. Which approach guarantees an optimal solution with O(n) time and O(1) space complexity?
easy
A. Use a recursive approach that reverses k nodes at a time and recurses on the rest, relying on call stack for reversal.
B. Apply a dynamic programming approach to store reversed sublists and combine them for the final result.
C. Iteratively traverse the list, reversing each group of k nodes using a helper function, and reconnect groups with pointer manipulation.
D. Use a greedy approach that reverses nodes whenever possible without checking group boundaries.

Solution

  1. Step 1: Understand problem constraints

    The problem requires reversing nodes in groups of k, preserving the order of leftover nodes if fewer than k remain.
  2. Step 2: Evaluate approaches

    Recursive approach (A) uses extra stack space and risks overflow; DP (B) is not applicable; greedy (D) fails on partial groups. Iterative with helper (C) reverses groups efficiently with O(1) space.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Iterative helper method is standard optimal solution [OK]
Hint: Iterative helper reversal is optimal for O(1) space [OK]
Common Mistakes:
  • Confusing recursion with iterative approach
  • Assuming DP applies to linked list reversal
  • Ignoring partial group handling