-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path328_Odd_Even_LL.cpp
64 lines (56 loc) · 1.29 KB
/
328_Odd_Even_LL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 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.
//The first node is considered odd, and the second node is even, and so on.
//Note that the relative order inside both the even and odd groups should remain as it was in the input.
//SC = O(1) and TC = O(n)
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int data) {
this-> data = data;
this-> next = NULL;
}
};
Node* oddEvenList(Node* head) {
if(head == NULL || head -> next == NULL) {
return head;
}
Node* oddHead = NULL;
Node* oddTail = NULL;
Node* evenHead = NULL;
Node* evenTail = NULL;
Node* curr = head;
int idx= 1;
while(curr != NULL) {
if( idx % 2== 1) {
if(oddTail == NULL) {
oddTail = curr;
oddHead = curr;
}
else{
oddTail-> next = curr;
oddTail = curr;
}
}
else {
if(evenTail == NULL) {
evenHead = curr;
evenTail = curr;
}
else{
evenTail -> next = curr;
evenTail= curr;
}
}
idx++;
curr = curr -> next;
}
evenTail -> next = NULL;
oddTail -> next = evenHead;
return oddHead;
}
int main() {
return 0;
}