Skip to content

Commit 7a78c86

Browse files
authored
Merge pull request #1384 from tanyagupta01/odd-even-linked-list
Create 0328-odd-even-linked-list.md
2 parents d0787a3 + a27f143 commit 7a78c86

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
id: odd-even-linked-list
3+
title: Odd Even Linked List
4+
sidebar_label: 0328 Odd Even Linked List
5+
tags:
6+
- Linked List
7+
- C++
8+
- Java
9+
- Python
10+
description: "This document provides a solution to group all the nodes with odd indices together followed by the nodes with even indices given the head of a singly linked list."
11+
---
12+
13+
## Problem
14+
Given the `head` of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
15+
16+
The first node is considered odd, and the second node is even, and so on.
17+
18+
Note that the relative order inside both the even and odd groups should remain as it was in the input.
19+
20+
You must solve the problem in `O(1)` extra space complexity and `O(n)` time complexity.
21+
22+
### Example 1:
23+
Input: head = [1,2,3,4,5]
24+
Output: [1,3,5,2,4]
25+
26+
### Example 2:
27+
Input: head = [2,1,3,5,6,4,7]
28+
Output: [2,3,6,7,1,5,4]
29+
30+
### Constraints:
31+
- The number of nodes in the linked list is in the range `[0, 10^4]`.
32+
- `-10^6 <= Node.val <= 10^6`
33+
34+
## Solution
35+
To reorder the given linked list based on odd and even indices, we can follow a simple approach. We'll divide the linked list into two separate groups: one for nodes with odd indices and one for nodes with even indices. Then, we'll merge these two groups to obtain the final reordered list while maintaining the relative order of nodes within each group. We'll start by initializing two pointers: one for odd nodes and one for even nodes. Initially, the odd pointer will point to the head of the linked list, and the even pointer will point to the next node. We'll also keep track of the head of the even list to connect it later. Next, we'll iterate through the linked list, advancing the odd pointer by two steps and the even pointer by two steps in each iteration. This will separate the nodes into odd and even group while keeping their original order.
36+
37+
Finally, we'll merge the even list after the odd list by linking the last node of the odd group to the head of the even group. This will connect the two groups and give us the desired reordered list.
38+
39+
#### Code in Different Languages
40+
41+
#### Python
42+
43+
```py
44+
class Solution:
45+
def oddEvenList(self, head: ListNode) -> ListNode:
46+
oddHead = ListNode(0)
47+
evenHead = ListNode(0)
48+
odd = oddHead
49+
even = evenHead
50+
isOdd = True
51+
52+
while head:
53+
if isOdd:
54+
odd.next = head
55+
odd = head
56+
else:
57+
even.next = head
58+
even = head
59+
head = head.next
60+
isOdd = not isOdd
61+
62+
even.next = None
63+
odd.next = evenHead.next
64+
return oddHead.next
65+
```
66+
67+
#### Java
68+
69+
```java
70+
class Solution {
71+
public ListNode oddEvenList(ListNode head) {
72+
ListNode oddHead = new ListNode(0);
73+
ListNode evenHead = new ListNode(0);
74+
ListNode odd = oddHead;
75+
ListNode even = evenHead;
76+
77+
for (boolean isOdd = true; head != null; head = head.next, isOdd = !isOdd)
78+
if (isOdd) {
79+
odd.next = head;
80+
odd = odd.next;
81+
} else {
82+
even.next = head;
83+
even = even.next;
84+
}
85+
86+
odd.next = evenHead.next;
87+
even.next = null;
88+
return oddHead.next;
89+
}
90+
}
91+
```
92+
93+
#### C++
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
ListNode* oddEvenList(ListNode* head) {
99+
ListNode oddHead(0);
100+
ListNode evenHead(0);
101+
ListNode* odd = &oddHead;
102+
ListNode* even = &evenHead;
103+
104+
for (int isOdd = 0; head; head = head->next)
105+
if (isOdd ^= 1) {
106+
odd->next = head;
107+
odd = odd->next;
108+
} else {
109+
even->next = head;
110+
even = even->next;
111+
}
112+
113+
odd->next = evenHead.next;
114+
even->next = nullptr;
115+
return oddHead.next;
116+
}
117+
};
118+
```
119+
120+
## References
121+
122+
- **LeetCode Problem:** [LeetCode Problem](https://leetcode.com/problems/odd-even-linked-list/)
123+
- **Solution Link:** [Shortest Palindrome Solution on LeetCode](https://leetcode.com/problems/odd-even-linked-list/solutions/)

0 commit comments

Comments
 (0)