Interleave Linked List Halves by Node Values
Linked ListsAuto-graded · 17 test casesLast updated
Tests pointer-manipulation intuition by reordering a linked list (given as an array) into a specific interleaved pattern derived from splitting and reversing halves.
Interleave Linked List Halves
You are given a singly linked list represented as a plain Python list of integers nodes. The list has n nodes with values nodes[0], nodes[1], ..., nodes[n-1].
Your task is to reorder the list in the following way:
- Split the list into two halves. If
nis odd, the first half gets the extra node.- First half: indices
0 .. ceil(n/2) - 1 - Second half: indices
ceil(n/2) .. n-1
- First half: indices
- Reverse the second half.
- Interleave the two halves: take one node from the first half, then one node from the (reversed) second half, alternating, until all nodes are used. Because the first half is longer or equal, any leftover node from the first half goes at the very end.
Return the reordered list of node values.
Details
- The operation is performed purely on the values; treat
nodesas a linked list whosenextpointers you are rearranging, but represent your answer as a Python list. - The interleaving picks alternately: first[0], second_rev[0], first[1], second_rev[1], …
- When
nis odd the first half has one more element than the second; that extra element is appended last.
Constraints
0 ≤ n ≤ 2000-10^6 ≤ nodes[i] ≤ 10^6- If
n == 0return[]. - If
n == 1return the single-element list unchanged. - Values are not necessarily distinct.
Examples
Example 1
Input: reorder_linked_list_halves([1, 2, 3, 4])
Output: [1, 4, 2, 3]
Example 2
Input: reorder_linked_list_halves([1, 2, 3, 4, 5])
Output: [1, 5, 2, 4, 3]
Example 3
Input: reorder_linked_list_halves([7])
Output: [7]
Hints
Hint 1
Find the midpoint using the classic slow/fast pointer technique (simulate it on the array with two indices).
Hint 2
Reverse only the second half in-place or into a new list, then merge the two halves with a two-pointer interleave.
Hint 3
Be careful about which half gets the extra node when n is odd — the first half does, and that lone extra node lands at the very end of the result.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Intuition
This problem simulates the classic "reorder list" linked-list interview problem but works on an array representation. The three conceptual steps map directly to pointer operations a candidate would perform on actual list nodes:
- Find the midpoint — equivalent to running slow/fast pointers until fast reaches the end.
- Reverse the second half — equivalent to reversing the
nextpointers of the tail segment. - Interleave — equivalent to splicing the two sub-lists together by alternating
nextassignments.
Step-by-step
Split: math.ceil(n / 2) gives the first-half length. For even n both halves are equal; for odd n the first half is one element longer.
Reverse: Slice second[::-1] (or equivalently, iterate from the back).
Interleave: Walk both halves with two pointers. Each iteration appends one element from each half. Because len(first) >= len(second_rev), only the first-half pointer can have a remainder, and it will have at most one element left (when n is odd).
Correctness
n = 0orn = 1: returned immediately without modification.- Even
n: both halves have equal length; the while loop exhausts both simultaneously; no remainder. - Odd
n:firsthas one extra element; the while loop exhaustssecond_revfirst; the trailingwhileappends the lone leftover.
Complexity
- Time: O(n) — one pass to split, one to reverse, one to interleave.
- Space: O(n) — the result list and the two auxiliary halves.
Reference solution
from typing import List
import math
def reorder_linked_list_halves(nodes: List[int]) -> List[int]:
n = len(nodes)
if n <= 1:
return list(nodes)
# Step 1: split — first half gets the extra node when n is odd
first_len = math.ceil(n / 2)
first = nodes[:first_len]
second = nodes[first_len:]
# Step 2: reverse the second half
second_rev = second[::-1]
# Step 3: interleave
result = []
i, j = 0, 0
while i < len(first) and j < len(second_rev):
result.append(first[i])
result.append(second_rev[j])
i += 1
j += 1
# Leftover from first half (at most one element, when n is odd)
while i < len(first):
result.append(first[i])
i += 1
return result