reorder-list
1.0.0
Reorder 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
void
reorderList
(
ListNode
* head)
21
{
22
if
(head ==
nullptr
)
23
return
;
24
deque<int> val_deque;
25
ListNode
* current = head->
next
;
26
while
(current !=
nullptr
)
27
{
28
val_deque.push_back(current->
val
);
29
current = current->
next
;
30
}
31
current = head;
32
while
(!val_deque.empty())
33
{
34
current->
next
=
new
ListNode
(val_deque.back());
35
val_deque.pop_back();
36
current = current->
next
;
37
if
(!val_deque.empty())
38
{
39
current->
next
=
new
ListNode
(val_deque.front());
40
val_deque.pop_front();
41
current = current->
next
;
42
}
43
}
44
current->
next
=
nullptr
;
45
return
;
46
}
47
};
48
49
int
main
()
50
{
51
52
return
0;
53
}
Solution
Definition
main.cpp:18
Solution::reorderList
void reorderList(ListNode *head)
Definition
main.cpp:20
main
int main()
Definition
main.cpp:49
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