Reorder Linked List Nodes by Alternating Ends
Linked ListsAuto-graded · 17 test casesLast updated
Tests pointer-manipulation intuition by reordering a singly linked list (given as an array) in a front-back-front-back zigzag pattern.
Reorder Linked List Nodes by Alternating Ends
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 pattern:
nodes[0] → nodes[n-1] → nodes[1] → nodes[n-2] → nodes[2] → nodes[n-3] → ...
That is, repeatedly pick from the front, then from the back, alternating, until all nodes have been placed.
Return the reordered list of values as a Python list.
Do not sort the values. You are only rearranging the existing nodes.
How the reordering works
Use two pointers — one starting at the front (lo = 0) and one at the back (hi = n-1). At each step:
- Take the element at
lo, advanceloforward. - If
lo <= hiafter step 1, take the element athi, movehibackward.
Repeat until all elements are consumed.
Constraints
0 <= n <= 2000-10^6 <= nodes[i] <= 10^6- If
n == 0, return[]. - If
n == 1, return the single-element list unchanged.
Examples
Example 1
Input: reorder_linked_list([1, 2, 3, 4, 5])
Output: [1, 5, 2, 4, 3]
Example 2
Input: reorder_linked_list([10, 20, 30, 40])
Output: [10, 40, 20, 30]
Example 3
Input: reorder_linked_list([7])
Output: [7]
Hints
Hint 1
Try using two index pointers — one at the start and one at the end of the list — and advance them inward while alternating which one you pull from.
Hint 2
Think about what happens when the list has an odd length versus an even length. The middle element (if it exists) will be picked last from the 'front' pointer, with no back-pointer element to pair it with.
Hint 3
You can simulate this without any actual linked list nodes: just build the result list by appending from lo, then from hi, advancing each pointer inward by one each round.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight mirrors how you would actually manipulate a linked list with two pointers: one starting at the head and one at the tail, walking inward until they meet.
Algorithm:
- Initialise
lo = 0,hi = n - 1. - While
lo <= hi:- Append
nodes[lo], incrementlo. - If
lo <= hi(guard against double-counting the middle element in an odd-length list), appendnodes[hi], decrementhi.
- Append
- Return the result list.
Why it is correct:
At every iteration we consume exactly the current front element and (if one remains) the current back element. The guard lo <= hi after the front pick ensures that the unique middle element of an odd-length list is only appended once. This exactly replicates what the problem statement describes.
Edge cases:
- Empty list → return
[]immediately. - Single element →
lo == hion the first iteration; we appendnodes[0], incrementloto 1, thenlo > hiso the guard prevents a second append. Result:[nodes[0]]. - Even length → every front pick is paired with a back pick; both pointers meet and cross cleanly.
- Odd length → the middle element is picked as a lone front pick in the last iteration.
Time complexity: O(n) — one pass through the list.
Space complexity: O(n) — for the output list (no additional auxiliary space beyond that).
from typing import List
def reorder_linked_list(nodes: List[int]) -> List[int]:
n = len(nodes)
if n == 0:
return []
result = []
lo, hi = 0, n - 1
while lo <= hi:
result.append(nodes[lo])
lo += 1
if lo <= hi:
result.append(nodes[hi])
hi -= 1
return result