0% completed
Problem Statement
Given the head
of a singly linked list, rearrange the nodes such that all nodes with odd
indices are grouped together followed by all nodes with even
indices, and return the updated list
The order of the nodes within the odd and even groups should remain as in the original list.
The reordering should be done in-place with O(1) extra space and O(n) time complexity.
Examples
Example 1:
- Input: head = [10, 20, 30, 40, 50, 60]
- Expected Output: [10, 30, 50, 20, 40, 60]
- Justification: Nodes at indices 1, 3, and 5 are grouped together first (10, 30, 50), followed by nodes at indices 2, 4, and 6 (20, 40, 60).
Example 2:
- Input: head = [1, 3, 5, 7, 9]
- Expected Output: [1, 5, 9, 3, 7]
- Justification: Nodes at indices 1, 3, and 5 are grouped together first (1, 5, 9), followed by nodes at indices 2 and 4 (3, 7).
Example 3:
- Input: head = [2, 4, 6, 8, 10, 12, 14]
- Expected Output: [2, 6, 10, 14, 4, 8, 12]
- Justification: Nodes at indices 1, 3, 5, and 7 are grouped together first (2, 6, 10, 14), followed by nodes at indices 2, 4, and 6 (4, 8, 12).
Constraints:
- The number of nodes in the linked list is in the range [0, 10<sup>4</sup>].
- -10<sup>6</sup> <= Node.val <= 10<sup>6</sup>
Solution
To solve this problem, we need to rearrange the linked list nodes so that nodes at odd indices come before nodes at even indices. We'll maintain two pointers: one for the odd-indexed nodes and one for the even-indexed nodes. By traversing the list once, we can link all odd-indexed nodes together, and then all even-indexed nodes. Finally, we'll link the last odd-indexed node to the first even-indexed node. This approach works efficiently because it only requires a single pass through the list and uses constant space.
This method is effective because it leverages the existing structure of the linked list and avoids additional space allocation, making it suitable for large datasets.
Step-by-Step Algorithm
-
Initialize:
- Set
odd
to point to the head of the list. - Set
even
to point to the second node. - Store the starting point of the even list in
even_head
.
- Set
-
Traverse the list:
- While there are more nodes to process:
- Set the next odd node to be the node after the next even node.
- Move the
odd
pointer to this next odd node. - Set the next even node to be the node after the next odd node.
- Move the
even
pointer to this next even node.
- While there are more nodes to process:
-
Combine lists:
- Set the next node of the last odd node to the head of the even list.
Algorithm Walkthrough
Let's walk through the example input [10, 20, 30, 40, 50, 60]
:
-
Initial setup:
odd
points to 10even
points to 20even_head
points to 20
-
First iteration:
- Link
odd.next
(10) toodd.next.next
(30) - Move
odd
to 30 - Link
even.next
(20) toeven.next.next
(40) - Move
even
to 40
- Link
-
Second iteration:
- Link
odd.next
(30) toodd.next.next
(50) - Move
odd
to 50 - Link
even.next
(40) toeven.next.next
(60) - Move
even
to 60
- Link
-
End of list:
- Link the last odd node (50) to the head of the even list (20)
Code
Complexity Analysis
Time Complexity
The algorithm traverses the linked list only once, where each node is visited and processed exactly once. Thus, the time complexity is O(n), where n is the number of nodes in the linked list.
Space Complexity
The algorithm rearranges the nodes in place without using any extra data structures that grow with the size of the input. Therefore, the space complexity is O(1).
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible