06,从尾部到头打印链表值

输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

 

示例 1:

输入:head = [1,3,2]
输出:[2,3,1]

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> res;
    vector<int> reversePrint(ListNode* head) {
        //方法1:reverse反转法
        /*
        while(head){
            res.push_back(head->val);
            head = head->next;
        }
        //使用algorithm算法中的reverse反转res
        reverse(res.begin(),res.end());
        return res;
        */

        //方法2:入栈法
        /*
        stack<int> s;
        //入栈
        while(head){
            s.push(head->val);
            head = head->next;
        }
        //出栈
        while(!s.empty()){
            res.push_back(s.top());
            s.pop();
        }
        return res;
        */

        //方法3:递归
        /*
        if(head == nullptr)
            return res;
        reversePrint(head->next);
        res.push_back(head->val);
        return res;
        */

        //方法4:改变链表结构
        ListNode *pre = nullptr;
        ListNode *tmp = nullptr;
        ListNode *cur = head;
        while(cur){
            tmp = cur->next;//保存当前结点的下一个节点
            cur->next = pre;//当前结点指向前一个节点,反向改变指针
            pre = cur;//更新前一个节点
            cur = tmp;//更新当前结点
        }
        while(pre){//上一个while循环结束后,pre指向新的链表头
            res.push_back(pre->val);
            pre = pre->next;
        }
        return res;
    }
};

 

posted @ 2021-07-27 09:38  刘凯_tju  阅读(41)  评论(0编辑  收藏  举报