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
3
using namespace
std;
4
8
struct
ListNode
9
{
10
int
val
;
11
ListNode
*
next
;
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
17
class
Solution
18
{
19
public
:
20
ListNode
*
oddEvenList
(
ListNode
* head)
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
57
int
main
()
58
{
59
60
return
0;
61
}
Solution
Definition
main.cpp:18
Solution::oddEvenList
ListNode * oddEvenList(ListNode *head)
Definition
main.cpp:20
main
int main()
Definition
main.cpp:57
ListNode
Definition for singly-linked list.
Definition
main.cpp:9
ListNode::val
int val
Definition
main.cpp:10
ListNode::ListNode
ListNode(int x)
Definition
main.cpp:13
ListNode::ListNode
ListNode()
Definition
main.cpp:12
ListNode::next
ListNode * next
Definition
main.cpp:11
ListNode::ListNode
ListNode(int x, ListNode *next)
Definition
main.cpp:14
main.cpp
Generated by
1.9.8