Facebook Pixel
Reorder List
Medium

Given the head of a singly linked list, reorder it to follow this pattern - the first node, then the last node, then the second node, then the second-last node, and so on. The reordering must be done in-place without modifying node values.

Example:

Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]

Input: head = [1,2,3,4]
Output: [1,4,2,3]
Test Cases

Test Cases

Input
1 2 3 4
Expected Output
1 4 2 3
Step 1
Step 2
Step 3
Step 4
Step 1: Identify the Pattern
Reorder List
Medium

Given the head of a singly linked list, reorder it to follow this pattern - the first node, then the last node, then the second node, then the second-last node, and so on. The reordering must be done in-place without modifying node values.

Example:

Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]

Input: head = [1,2,3,4]
Output: [1,4,2,3]
Test Cases

Test Cases

Input
1 2 3 4
Expected Output
1 4 2 3
Step 1
Step 2
Step 3
Step 4
Step 1: Identify the Pattern