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
INPUTALGORITHMRESULTLinear Linked List12345Sequential: 1→2→3→4→51Find Middle2Reverse Second Half3Merge AlternatelySplit: [1,2] | [3,4,5]Reverse: [1,2] | [5,4,3]Zipper: 1→5→2→4→3Zipper Pattern15243Perfect alternation!Key Insight:Split the list at middle, reverse second half, then merge alternately like shuffling cardsTutorialsPoint - Linked List Zipper | Three-Step In-Place Algorithm
Asked in
Facebook 35 Amazon 28 Microsoft 22 Google 18
156.2K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen