Interleave Two Linked Lists from Their Midpoints
Linked ListsAuto-graded · 17 test casesLast updated
Tests pointer-manipulation reasoning by reordering a singly linked list (given as an array) so nodes alternate between the first and second halves, starting from their respective midpoints.
Interleave Two Linked Lists from Their Midpoints
You are given a singly linked list represented as a plain Python list nodes, where nodes[i] is the value of the i-th node (0-indexed, head at index 0).
Your task is to reorder the list in-place (return the reordered values as a new list) according to the following rule:
-
Split the list into two halves:
- First half: indices
0 … m-1 - Second half: indices
m … n-1
where
m = ceil(n / 2)andn = len(nodes).
(If the list has an odd number of nodes, the first half gets the extra node.) - First half: indices
-
Reverse the second half.
-
Interleave the two halves by alternating one node from the first half and one node from the (reversed) second half:
first[0], second_rev[0], first[1], second_rev[1], …Because the first half is at least as long as the second half, if the first half has any leftover nodes after the second half is exhausted, append them at the end.
Return the reordered list of values.
Example walkthrough
For nodes = [1, 2, 3, 4, 5]:
- First half:
[1, 2, 3], second half:[4, 5] - Reversed second half:
[5, 4] - Interleaved:
[1, 5, 2, 4, 3]
Constraints
0 ≤ len(nodes) ≤ 2000-10^6 ≤ nodes[i] ≤ 10^6- If
len(nodes) == 0orlen(nodes) == 1, return the list unchanged. - Values may contain duplicates.
Examples
Example 1
Input: interleave_from_midpoint([1, 2, 3, 4, 5])
Output: [1, 5, 2, 4, 3]
Example 2
Input: interleave_from_midpoint([1, 2, 3, 4])
Output: [1, 4, 2, 3]
Example 3
Input: interleave_from_midpoint([7])
Output: [7]
Hints
Hint 1
Find the midpoint using integer ceiling division: m = (n + 1) // 2.
Hint 2
Reverse the second half slice in place (or use Python slicing [::-1]) before interleaving.
Hint 3
Use two pointers—one walking the first half, one walking the reversed second half—and zip them together, then append any remainder from the first half.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Step-by-step reasoning
-
Edge cases: An empty list or single-element list is returned as-is — there is nothing to split or interleave.
-
Split: Compute
m = (n + 1) // 2(ceiling ofn / 2). Slicefirst = nodes[:m]andsecond = nodes[m:]. This guaranteeslen(first) >= len(second)always. -
Reverse the second half:
second[::-1]gives the reversed second half. This is the key "pointer manipulation" step — in a real linked list you would walk to the tail and reverse thenextpointers. -
Interleave: Walk both halves in lockstep with index
i. For each position, emitfirst[i]thensecond[i]. Becauselen(first) >= len(second), oncesecondis exhausted we simply append the remaining elements offirst.
Why it is correct
- Ceiling division ensures that when
nis odd the extra node stays in the first half, sofirstis always at least as long assecond. - Reversing the second half and interleaving matches the stated rule exactly.
- The leftover-append step handles both even-length lists (no leftover) and odd-length lists (exactly one leftover from
first).
Complexity
| Value | |
|---|---|
| Time | O(n) — one pass to slice, one to reverse, one to interleave |
| Space | O(n) — for the output list and the two half-slices |
Reference solution
from typing import List
def interleave_from_midpoint(nodes: List[int]) -> List[int]:
n = len(nodes)
if n <= 1:
return list(nodes)
m = (n + 1) // 2 # ceiling division: first half length
first = nodes[:m]
second = nodes[m:][::-1] # reversed second half
result = []
for i in range(len(second)):
result.append(first[i])
result.append(second[i])
# Append any leftover nodes from the first half
result.extend(first[len(second):])
return result