第三 题 剑指offer 链表的逆序打印

xiaoxiao2021-02-28  3

/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int >result ; stack<int>st; if(head==nullptr) return result; ListNode *p=head; while(p) { st.push(p->val); p=p->next; } while(st.size()) { int temp=st.top(); result.push_back(temp); st.pop(); } return result; } };
转载请注明原文地址: https://www.6miu.com/read-1749977.html

最新回复(0)