-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodd_even_linked_list.cpp
More file actions
43 lines (39 loc) · 1.13 KB
/
Copy pathodd_even_linked_list.cpp
File metadata and controls
43 lines (39 loc) · 1.13 KB
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
#include <iostream>
using namespace std;
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) {
ListNode *cur = head, *odd_branch = NULL, *even_branch = NULL, *curnext = NULL;
odd_branch = head;
even_branch = odd_branch->next;
while (cur->next != NULL) {
curnext = cur->next;
// do smth here
cur->next = cur->next->next;
cur = curnext;
}
ListNode *odd_branch_tail = odd_branch;
while (odd_branch_tail->next) {
odd_branch_tail = odd_branch_tail->next;
}
odd_branch_tail->next = even_branch;
for (ListNode *i = odd_branch; i != NULL; i = i->next) {
cout << i->val << " ";
}
cout << endl;
return NULL;
}
};
int main() {
Solution solution;
ListNode* ll = new ListNode(1, new ListNode(2, new ListNode(3)));
solution.oddEvenList(ll);
return 0;
}