Article Directory
THE
problem
solution
Code
/*
思路: 翻转链表之后再去打印。 属于翻转类问题。
-
-
- -
- -
-
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> ans;
ListNode* pre =NULL, *temp =NULL;
while(head){
temp = head->next;
head->next =pre;
pre = head;
head = temp;
}
while(pre){
ans.push_back(pre->val);
pre = pre->next;
}
return ans;
}
};
Summary and reflection
- Pay attention to consideration of boundary issues.