从尾到头打印链表

来源:互联网 发布:中国数据域名解析 编辑:程序博客网 时间:2024/06/12 21:41

题目描述

输入一个链表,从尾到头打印链表每个节点的值。返回新链表的头结点。

/***  struct ListNode {*       int val;*       struct ListNode *next;*       ListNode(int x) :*             val(x), next(NULL) {*       }*  };*/ class Solution {public:  vector<int> printListFromTailToHead(struct ListNode* head) {    vector<int> a;        if(head!=NULL)        {            if(head->next!=NULL)            {                a=printListFromTailToHead(head->next);            }            a.push_back(head->val);        }        return a;  }};
0 0