《剑指offer》从尾到头打印链表

题目描述

输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

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

猜你喜欢

转载自blog.csdn.net/qq_42633819/article/details/83004539
今日推荐