Skip to content

Commit 19cf890

Browse files
Merge pull request #62 from PawanJaiswal0843/master
238.odd-even-linked-list.cpp
2 parents b24236d + d4d8b92 commit 19cf890

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

238.odd-even-linked-list.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// 328. Odd Even Linked List
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
class ListNode {
6+
public:
7+
int data;
8+
ListNode* next;
9+
10+
public:
11+
ListNode(int data){
12+
this->data = data;
13+
this->next = NULL;
14+
}
15+
};
16+
17+
ListNode* oddEvenList(ListNode* head) {
18+
19+
if(head == NULL || head->next == NULL || head->next->next == NULL)
20+
return head;
21+
22+
ListNode* end = head;
23+
int count = 0;
24+
25+
while(end->next != NULL) {
26+
end = end->next;
27+
count++;
28+
}
29+
30+
int counter = (count & 1) ? (count/2 + 1) : (count/2);
31+
ListNode* temp = head;
32+
33+
while(counter--) {
34+
35+
end->next = temp->next;
36+
temp->next = temp->next->next;
37+
end->next->next = NULL;
38+
temp = temp->next;
39+
end = end->next;
40+
}
41+
42+
return head;
43+
}

0 commit comments

Comments
 (0)