-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path从尾到头打印链表.cpp
More file actions
34 lines (30 loc) · 796 Bytes
/
Copy path从尾到头打印链表.cpp
File metadata and controls
34 lines (30 loc) · 796 Bytes
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
/* ********************
* 从尾到头打印链表(链表)
* 输入一个链表,从尾到头打印链表每个节点的值。
* 返回新链表的头结点。
* *******************/
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
vector<int> printListFromTailToHead(struct ListNode* head) {
stack<int> dataStack;
vector<int> dataVector;
struct ListNode *tempNode = head;
while(tempNode!=NULL){
dataStack.push(tempNode->val);
tempNode = tempNode->next;
}
while(!dataStack.empty()){
int data = dataStack.top();
dataStack.pop();
dataVector.push_back(data);
}
return dataVector;
}
};