首页 > 其他 > 详细

从尾到头打印链表

时间:2019-11-28 00:39:41      阅读:74      评论:0      收藏:0      [点我收藏+]

题目描述

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
 
题解:每次插入到vector的头部直到head==NULL;
 
参考代码:
技术分享图片
 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(ListNode* head) 
13     {
14         vector<int> ans;
15         while(head!=NULL)
16         {
17             ans.insert(ans.begin(),head->val);
18             head=head->next;
19         }
20         return ans;
21         
22     }
23 };
View Code

 

从尾到头打印链表

原文:https://www.cnblogs.com/csushl/p/11946339.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!