zoukankan      html  css  js  c++  java
  • 输入一个链表,从尾到头打印链表每个节点的值。

    从尾到头,可以通过栈临时存储:

    /**
    *  struct ListNode {
    *        int val;
    *        struct ListNode *next;
    *        ListNode(int x) :
    *              val(x), next(NULL) {
    *        }
    *  };
    */
    class Solution {
    public:
        vector<int> printListFromTailToHead(ListNode* head) {
            vector<int> list;
            while(head != NULL)
            {
                list.push_back(head->val);
                head = head->next; 
            }
            reverse(list.begin(),list.end());       //reverse()函数用于反转vector数组   
            return list;
        }
    };
    既然选择了远方,便只顾风雨兼程
  • 相关阅读:
    E
    C
    航空母舰-03
    航空母舰-02
    航空母舰-01
    新概念4-30
    html
    翁凯-编程学习方法
    机器学习Ng-02
    民法-钟秀勇-导学
  • 原文地址:https://www.cnblogs.com/Forever-Road/p/6486424.html
Copyright © 2011-2022 走看看