标签:
输入为链表的表头
输出为需要打印的“新链表”的表头
1 /** 2 * struct ListNode { 3 * int val; 4 * struct ListNode *next; 5 * ListNode(int x) : 6 * val(x), next(NULL) { 7 * } 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> printListFromTailToHead(struct ListNode* head) { 13 std::stack<ListNode*> nodes; 14 ListNode *pNode = head; 15 while(pNode != NULL) 16 { 17 nodes.push(pNode); 18 pNode = pNode->next; 19 } 20 vector<int> result; 21 while(!nodes.empty()) 22 { 23 pNode = nodes.top(); 24 result.push_back(pNode->val); 25 nodes.pop(); 26 } 27 return result; 28 } 29 };
标签:
原文地址:http://www.cnblogs.com/xiaoyesoso/p/5142527.html