这段代码是利用“头插法”逆转链表,我试了很多例子都对,但是提交显示“段错误”只有一个例子没通过,各位能帮忙看看问题出在哪?
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
ListNode *p, *tmp;
p=head->next;
head->next=NULL;
while(p) {
tmp = p;
p=p->next;
tmp->next=head;
head=tmp; }
vector<int> res;
while(tmp){
res.push_back(tmp->val);
tmp = tmp -> next;
}
return res;
}};