且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从尾到头打印链表

更新时间:2022-09-11 07:55:32

C++

从尾到头打印链表
 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         vector<int> res;
14         vector<int>::iterator it;
15         ListNode *idx = head;
16         while (idx) {
17             it = res.begin();
18             res.insert(it, idx->val);
19             idx = idx->next;
20         }
21         return res;
22     }
23 };
从尾到头打印链表

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5110551.html,如需转载请自行联系原作者