Weave Two Halves of a Linked List
Linked ListsAuto-graded · 17 test casesLast updated
Tests pointer manipulation intuition by reordering a singly linked list represented as an array, interleaving nodes from the front and back halves.
Weave Two Halves of a Linked List
You are given a singly linked list represented as an array nodes, where nodes[i] is the value of the i-th node (0-indexed, from head to tail).
Your task is to reorder the list in-place using the following rule:
Interleave nodes taken alternately from the front and the back of the original list, always picking from the front first.
Concretely, if the original list has nodes at indices 0, 1, 2, ..., n-1, the reordered list should be:
index 0, index n-1, index 1, index n-2, index 2, index n-3, ...
Return the reordered list as a new array.
Details:
- The "front pointer" starts at index
0and moves right. - The "back pointer" starts at index
n-1and moves left. - At each step, append the front element, then the back element (if the two pointers haven't crossed or met).
- Stop adding elements once every original node has been placed exactly once.
- If
nodesis empty, return an empty list.
Constraints
0 <= len(nodes) <= 2000-10^6 <= nodes[i] <= 10^6- Each node value is an integer.
Examples
Example 1
Input: weave_halves([1, 2, 3, 4, 5])
Output: [1, 5, 2, 4, 3]
Example 2
Input: weave_halves([1, 2, 3, 4])
Output: [1, 4, 2, 3]
Example 3
Input: weave_halves([])
Output: []
Hints
Hint 1
Use two pointers, one starting at the beginning and one at the end, and advance them toward each other.
Hint 2
Be careful about the stopping condition: once the front pointer index is >= the back pointer index, you should stop appending the back element to avoid duplicating the middle node of an odd-length list.
Hint 3
Simulate the process with a small odd-length example (e.g., [1,2,3,4,5]) and a small even-length example (e.g., [1,2,3,4]) to verify your logic handles the middle node correctly.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Intuition
Represent the linked list as an array and use two pointers — left starting at the head (index 0) and right starting at the tail (index n-1). Repeatedly:
- Append
nodes[left]and advanceleftforward. - If
left <= rightstill holds (i.e., there is still a distinct back element remaining), appendnodes[right]and advancerightbackward.
The guard left <= right before the second append is critical: for an odd-length list the two pointers eventually land on the same index (the middle element). That element is already appended in step 1, so we must not append it again in step 2.
Correctness
- Even length (n=4): indices visited are
(0,3), (1,2)— all 4 nodes placed once. ✓ - Odd length (n=5): indices visited are
(0,4), (1,3), thenleft=2, right=2— appendnodes[2]in step 1, guard fails for step 2 → all 5 nodes placed once. ✓ - Single element:
left=right=0→ append once, guard fails →[nodes[0]]. ✓ - Empty list: early return
[]. ✓
Complexity
| Value | |
|---|---|
| Time | O(n) — one pass through the array |
| Space | O(n) — the output array |
Reference Solution
from typing import List
def weave_halves(nodes: List[int]) -> List[int]:
if not nodes:
return []
result = []
left = 0
right = len(nodes) - 1
while left <= right:
result.append(nodes[left])
left += 1
if left <= right: # guard: don't re-add the middle element
result.append(nodes[right])
right -= 1
return result