odd-even-linked-list 1.0.0
Odd Even Linked List
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <bits/stdc++.h>
2
3using namespace std;
4
8struct ListNode
9{
10 int val;
12 ListNode() : val(0), next(nullptr) {}
13 ListNode(int x) : val(x), next(nullptr) {}
14 ListNode(int x, ListNode *next) : val(x), next(next) {}
15};
16
18{
19public:
21 {
22 if (!head)
23 return NULL;
24 list<ListNode*> odd;
25 list<ListNode*> even;
26 ListNode* current = head;
27 bool is_odd = true;
28 while (current != NULL)
29 {
30 if (is_odd)
31 odd.push_back(current);
32 else
33 even.push_back(current);
34 current = current->next;
35 is_odd = !is_odd;
36 }
37
38 current = head;
39 auto it = odd.begin();
40 it++;
41 for (; it != odd.end(); ++it)
42 {
43 current->next = *it;
44 current = current->next;
45 }
46 it = even.begin();
47 for (; it != even.end(); ++it)
48 {
49 current->next = *it;
50 current = current->next;
51 }
52 current->next = NULL;
53 return head;
54 }
55};
56
57int main()
58{
59
60 return 0;
61}
ListNode * oddEvenList(ListNode *head)
Definition main.cpp:20
int main()
Definition main.cpp:57
Definition for singly-linked list.
Definition main.cpp:9
int val
Definition main.cpp:10
ListNode(int x)
Definition main.cpp:13
ListNode()
Definition main.cpp:12
ListNode * next
Definition main.cpp:11
ListNode(int x, ListNode *next)
Definition main.cpp:14