【劍指offer】面試題06.從尾到頭打印鏈表

類別

鏈表

代碼

class Solution {
public:
    vector<int> reversePrint(ListNode* head) {
        ListNode *prev, *cur,*temp;//prev指向當前結點的前驅結點,cur指向當前結點,temp存儲當前結點的下一節點
        prev = NULL;
        cur = head;
        while (cur)
        {
            temp = cur->next;
            cur->next = prev;
            prev = cur;
            cur = temp;
        }
        head = prev;
        vector<int> list;
        while (head)
        {
            list.push_back(head->val);
            head = head->next;
        }
        return list;
    }
};
發佈了99 篇原創文章 · 獲贊 19 · 訪問量 8176
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章