Interleave Two Halves of a Linked List (Array Form)
Linked ListsAuto-graded · 19 test casesLast updated
Tests pointer-manipulation thinking by reordering a node list so that the first half and reversed second half are interleaved, all represented as arrays of values.
Interleave Two Halves of a Linked List
You are given a singly linked list represented as a plain Python list of integers nodes, where nodes[i] is the value of the i-th node in order.
Your task is to reorder the list in-place (return a new list) so that the final order follows the pattern:
L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …
In other words, repeatedly take one node from the front of the original list, then one node from the back, until all nodes are consumed.
- If
nodesis empty, return[]. - If
nodeshas one element, return it unchanged. - For an odd number of nodes, the middle node appears in the correct interleaved position (front pointer reaches it last before the two pointers meet).
No sorting is performed; only the positions of the existing values change.
Constraints
0 ≤ len(nodes) ≤ 2000-10^6 ≤ nodes[i] ≤ 10^6- Values are not necessarily distinct.
- You must return a new list; do not modify the input.
Examples
Example 1
Input: interleave_halves([1, 2, 3, 4, 5])
Output: [1, 5, 2, 4, 3]
Example 2
Input: interleave_halves([1, 2, 3, 4])
Output: [1, 4, 2, 3]
Example 3
Input: interleave_halves([42])
Output: [42]
Hints
Hint 1
Use two pointers (left starting at 0, right starting at len-1) and advance them toward each other, appending alternately to a result list.
Hint 2
Be careful about the termination condition: stop when left >= right to avoid adding the middle element twice in the odd-length case.
Hint 3
Trace through a small example like [1,2,3,4,5]: take 1 (front), 5 (back), 2 (front), 4 (back), 3 (middle) → [1,5,2,4,3].
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Intuition
Simulating a linked-list reorder with two pointers on the array is the cleanest way to think about this problem. Imagine holding the list at both ends:
- Pick from the left (front of list).
- Pick from the right (back of list).
- Advance both pointers inward and repeat until they meet.
When the list has an odd length the two pointers end up pointing at the same (middle) element; that element is appended once at the end.
Why It Is Correct
The pattern L0, Ln, L1, Ln-1, … is exactly what alternating front/back picks produce. After each pair the subproblem shrinks by 2 (one from each end), so every element is visited exactly once. The left == right guard after the loop handles the odd-length middle element without double-counting.
Complexity
- Time: O(n) — one linear pass with two pointers.
- Space: O(n) — the result list holds all n elements (no in-place mutation of the input as required by the problem).
Reference Solution
from typing import List
def interleave_halves(nodes: List[int]) -> List[int]:
if not nodes:
return []
result = []
left, right = 0, len(nodes) - 1
while left < right:
result.append(nodes[left])
result.append(nodes[right])
left += 1
right -= 1
# Odd-length: middle element hasn't been added yet
if left == right:
result.append(nodes[left])
return result
Common Mistakes to Watch Out For
- Using
left <= rightinstead ofleft < rightcauses the middle element of an odd-length list to be appended twice (once as a "front" pick and once as a "back" pick). - Forgetting the post-loop
if left == rightcheck leaves the middle element out entirely for odd-length inputs. - Modifying
nodesin place (forbidden by the spec) rather than building a new list.