Linked List Zipper - Problem
Given a singly linked list L0 → L1 → L2 → ... → Ln-1 → Ln, reorder it to: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → ...
The reordering should alternate between nodes from the beginning and nodes from the end of the list, creating a "zipper" pattern.
Important: You must do this in-place without changing the values in the list's nodes.
Examples:
[1,2,3,4]becomes[1,4,2,3][1,2,3,4,5]becomes[1,5,2,4,3]
Input & Output
Example 1 — Basic Odd Length
$
Input:
head = [1,2,3,4,5]
›
Output:
[1,5,2,4,3]
💡 Note:
Zipper pattern: take from start (1), then end (5), then start (2), then end (4), finally middle (3)
Example 2 — Even Length
$
Input:
head = [1,2,3,4]
›
Output:
[1,4,2,3]
💡 Note:
Alternate between start and end: 1 (start), 4 (end), 2 (start), 3 (end)
Example 3 — Single Node
$
Input:
head = [1]
›
Output:
[1]
💡 Note:
Single node remains unchanged
Constraints
- 1 ≤ Number of nodes ≤ 5 × 104
- 1 ≤ Node.val ≤ 1000
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code