-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmain.cpp
62 lines (55 loc) · 1.15 KB
/
main.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
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if (head == NULL) {
return NULL;
}
ListNode *oddHead, *oddTail, *evenHead, *evenTail;
oddHead = new ListNode(0);
evenHead = new ListNode(0);
oddTail = oddHead;
evenTail = evenHead;
ListNode* curr = head;
int counter = 1;
while (curr != NULL) {
if (counter % 2 == 0) {
evenTail->next = curr;
evenTail = evenTail->next;
} else {
oddTail->next = curr;
oddTail = oddTail->next;
}
curr = curr->next;
++counter;
}
oddTail->next = evenHead->next;
evenTail->next = NULL;
return oddHead->next;
}
};
int main() {
Solution sol;
sol.oddEvenList(NULL);
ListNode head(1);
head.next = new ListNode(2);
sol.oddEvenList(&head);
return 0;
}