-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path328.cpp
More file actions
33 lines (33 loc) · 749 Bytes
/
Copy path328.cpp
File metadata and controls
33 lines (33 loc) · 749 Bytes
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
// simple.cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *oddEvenList(ListNode *head) {
if (!head || !head->next)
return head;
ListNode odd(0), even(0);
odd.next = head;
even.next = head->next;
ListNode *odd_tail = odd.next;
ListNode *even_tail = even.next;
head = head->next->next;
while (head) {
odd_tail = odd_tail->next = head;
head = head->next;
if (!head)
break;
even_tail = even_tail->next = head;
head = head->next;
}
odd_tail->next = even.next;
even_tail->next = nullptr;
return odd.next;
}
};