Title Description:
"Enter a list to print the values of each node of the linked list from the end of the head. ”
This is the first question in the series of "sword Point offer", the good beginning is half the success. However, I submitted it once, wa! Re-submit, wa! Com ' on!
It seems that my start is not smooth. But what I want is not half the success, such a beginning how can stop me the road of AC! Looked carefully over the topic requirements and looked at the submission format. Submit again!
finally,ac!
The code is as follows:
1 /**2 * struct ListNode {3 * int val;4 * struct ListNode *next;5 * ListNode (int x):6 * val (x), Next (NULL) {7 * }8 * };9 */Ten classSolution { One Public: Avector<int> Printlistfromtailtohead (structlistnode*head) { -vector<int>nodes; -listnode* Pcurtnode =head; the while(pcurtnode) { -Nodes.push_back (pcurtnode->val); -Pcurtnode = pcurtnode->Next; - } +can also be reversed in nodes. -vector<int>Answer (Nodes.rbegin (), Nodes.rend ()); Reverse + returnanswer; A } at};
The stack of the front xlshao* used to save the history node, I used the vector directly, and then the reverse is OK (of course, can be directly in the nodes reversal)
Sword Point Offer | Print linked list from tail to head