-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16 - Odd Even Linked List.cpp
39 lines (38 loc) · 1.05 KB
/
16 - Odd Even Linked List.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(head == NULL || head->next == NULL || head->next->next == NULL){
return head;
}
ListNode* temp1 = new ListNode(head->val);
ListNode* temp3 = temp1;
head = head->next;
ListNode* temp2 = new ListNode(head->val);
ListNode* temp4 = temp2;
int i = 3;
while(head->next != NULL){
head = head->next;
if(i%2 == 1){
temp1->next = new ListNode(head->val);
temp1 = temp1->next;
}
else{
temp2->next = new ListNode(head->val);
temp2 = temp2->next;
}
i++;
}
temp1->next = temp4;
return temp3;
}
};