-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03.cpp
More file actions
68 lines (63 loc) · 1.05 KB
/
03.cpp
File metadata and controls
68 lines (63 loc) · 1.05 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
vector<int> dev;
stack<int> s;
struct ListNode {
int val;
ListNode* next;
ListNode(int x) :val(x), next(NULL) {}
ListNode() {}
};
vector<int> printListFromTailToHead(ListNode* head)
{
/*if (head != NULL) {
if (head->next != NULL) {
dev = printListFromTailToHead(head->next);
}
dev.push_back(head->val);
}*/
if (head != NULL)
{
s.push(head->val);
while ((head = head->next) != NULL)
{
s.push(head->val);
}
while (!s.empty())
{
dev.push_back(s.top());
s.pop();
}
}
return dev;
}
int main()
{
ios::sync_with_stdio(false);
int n, flag = true;
cin >> n;
ListNode* head = new ListNode;
ListNode* L = new ListNode;
for (int i = 0; i < n; i++)
{
ListNode* p = new ListNode;
cin >> p->val;
if (flag)
{
head = p;
flag = false;
}
p->next = L;
L->next = p;
L = p;
}
L->next = NULL;
printListFromTailToHead(head);
vector<int>::iterator it;
for (it = dev.begin(); it != dev.end(); ++it)
cout << *it << " ";
cout << endl;
return 0;
}