143. Reorder List - Detailed Explanation
Problem Statement
Description:
You are given the head of a singly linked list:
L0 → L1 → … → Ln - 1 → Ln
Reorder the list to:
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
You must do this in-place without altering the node values (i.e., you can only change the pointers).
Examples:
-
Example 1:
Input:
head = [1, 2, 3, 4]
Output:
[1, 4, 2, 3]
Explanation:
The list is reordered by alternating nodes from the start and end: first node (1), last node (4), second node (2), then third node (3). -
Example 2:
Input:
head = [1, 2, 3, 4, 5]
Output:
[1, 5, 2, 4, 3]
Explanation:
The list is reordered to: first node (1), last node (5), second node (2), second-to-last node (4), and finally the middle node (3).
Constraints:
- The number of nodes in the list is in the range
[1, 5 * 10⁴]
. - The node values can be any valid integers.
Hints Before Diving into the Solution
-
Hint 1:
One way to think about the problem is to split it into parts. First, identify the middle of the list. Then, reverse the second half and merge the two halves alternately. -
Hint 2:
While a brute-force solution might use extra space (like an array or list to store nodes), consider how you can do it in-place by manipulating the pointers directly. Think about the classic "fast and slow pointers" technique to find the middle.
Approaches
Approach 1: Using Extra Space
Idea:
- Traverse and Store Nodes:
Store all the nodes in an array or list. - Reorder Using Two Pointers:
Use two pointers (one starting at the beginning and one at the end) to select nodes alternately. - Relink Nodes:
Iterate through the list of nodes and relink them in the new order.
Drawbacks:
- Space Complexity:
This approach uses extra space proportional to the number of nodes, which violates the in-place requirement for this problem.
Approach 2: Optimal In-Place Solution
Idea:
This approach involves three main steps:
-
Find the Middle:
Use the fast and slow pointer technique to find the middle of the linked list. -
Reverse the Second Half:
Reverse the linked list starting from the middle (or the node right after the middle) to the end. -
Merge the Two Halves:
Merge the first half and the reversed second half alternately.
Detailed Steps:
-
Step 1 (Find Middle):
Initialize two pointers:slow
andfast
. Moveslow
by one step andfast
by two steps untilfast
reaches the end. Nowslow
points to the middle. -
Step 2 (Reverse Second Half):
Starting fromslow.next
, reverse the second half of the list. Setslow.next
tonull
to break the list into two parts. -
Step 3 (Merge Halves):
Use two pointers—one for the first half and one for the reversed second half—and alternate linking nodes until you finish merging.
Visual Example:
Consider the list: 1 → 2 → 3 → 4 → 5
-
Find the Middle:
- Fast and slow pointers yield
slow
at node3
.
- Fast and slow pointers yield
-
Reverse Second Half:
- The second half
4 → 5
is reversed to5 → 4
.
- The second half
-
Merge Halves:
- Merge nodes alternately:
- Start with node
1
, then node5
, then node2
, then node4
, and finally node3
.
- Start with node
- Merge nodes alternately:
Final reordered list: 1 → 5 → 2 → 4 → 3
Code Implementation
Python Code
Java Code
Complexity Analysis
-
Time Complexity:
- Finding the Middle: O(N), where N is the number of nodes.
- Reversing the Second Half: O(N/2) → O(N).
- Merging the Halves: O(N).
Overall, the time complexity is O(N).
-
Space Complexity:
- The algorithm uses only a few pointers and performs the operations in-place.
Thus, the space complexity is O(1).
Step-by-Step Walkthrough & Visual Example
Consider the list: 1 → 2 → 3 → 4 → 5
-
Find the Middle:
- Use fast and slow pointers. When
fast
reaches the end,slow
is at3
.
- Use fast and slow pointers. When
-
Reverse the Second Half:
- The second half starting at
4 → 5
is reversed to5 → 4
. - Now the list is split into two parts:
- First half:
1 → 2 → 3
- Second half (reversed):
5 → 4
- First half:
- The second half starting at
-
Merge the Two Halves:
- Alternate linking nodes:
- Start with
1
, then5
, then2
, then4
, and finally attach3
.
- Start with
- Alternate linking nodes:
Resulting list: 1 → 5 → 2 → 4 → 3
Common Mistakes
-
Incorrect Middle Calculation:
Not properly splitting the list at the middle can lead to errors during reversal or merge. -
Reverse Implementation:
Ensure the reversal is done correctly; otherwise, the merge step might not work as expected. -
Merging Logic:
Be careful when alternating nodes to avoid creating cycles or losing parts of the list.
Edge Cases & Alternative Variations
-
Edge Case 1:
A list with only one node or two nodes. The algorithm should handle these without changes. -
Alternative Variation:
Some variations may allow using extra space. However, the optimal solution works in-place and is generally preferred in interviews.
Related Problems for Further Practice
-
Reverse Linked List:
Practice reversing a linked list, which is a sub-problem of this challenge. -
Middle of the Linked List:
Finding the middle node using fast and slow pointers is a common technique. -
Merge Two Sorted Lists:
Although the merging order is different, merging two lists is a useful practice problem.
GET YOUR FREE
Coding Questions Catalog
