当前位置:网站首页>Sword finger offer 06 Print linked list from end to end

Sword finger offer 06 Print linked list from end to end

2022-06-11 08:55:00 Drag is me

leetcode Force button to brush questions and punch in

subject : The finger of the sword Offer 06. Print linked list from end to end
describe : Enter the head node of a linked list , Return the value of each node from the end to the end ( Return with array ).

Their thinking

1、 Traversing the linked list , Store the value of each linked list node in the array , And then use reverse Function invert array ;

Source code ##

class Solution {
    
public:
    vector<int> reversePrint(ListNode* head) {
    
        vector<int>v;
        while (head) {
    
            v.emplace_back(head->val);
            head = head->next;
        }
        reverse(v.begin(), v.end());
        return v;
    }
};
原网站

版权声明
本文为[Drag is me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110855012122.html