File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments