day 3 从尾到头打印

来源:互联网 发布:淘宝店铺头像怎么修改 编辑:程序博客网 时间:2024/06/09 20:15

题目: 输入一个链表,从尾到头打印链表每个节点的值。

思路:递归(栈)


代码:class Solution {
public:
  
    vector<int> res;
    vector<int>& printListFromTailToHead(ListNode* head) {
        //day 3 周一 2017-6-12日
        // 递归解决(栈)
        // 声明个存储的vector
         vector<int> res;
       // ListNode *p =head;
        //while(p!=NULL)
        if(head!=NULL)
            {
            //不为空,往下走
             
            if(head->next!=NULL)   res=printListFromTailToHead(head->next);
            //打印
            res.push_back(head->val);
            }
         
       //   return res;
 
       return res;


        
    }
};

原创粉丝点击