有人知道这个栈实现链表反转为啥不行吗
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
stack<ListNode*> s;
ListNode* ret =new ListNode(-1) ;//问题出在这???
ListNode* temp;
ret=temp;
if(pHead==NULL) return pHead;
while(pHead!=NULL)
{
s.push(pHead);
pHead=pHead->next;
}
temp=ret;
while(!s.empty())
{
temp->next=s.top();
temp=temp->next;
s.pop();
}
temp->next=NULL;
return ret->next;
}
};#笔试题目#
查看7道真题和解析

