#include<iostream>
#include<stack>
using namespace std;
struct ListNode
{
ListNode *next;
int data;
ListNode(int x):
{
data=x;
next=NULL:
}
};
ListNode* ReverseList(ListNode* pHead) {
if(pHead==NULL||pHead->next == NULL)
{
return pHead;
}
ListNode * p=pHead;
ListNode * newHead;
stack<ListNode *> stack1;
while(p->next!=NULL)
{
stack1.push(p);
p=p->next;
}
newHead = p;
while(!stack1.empty())
{
p->next=stack1.top();
p=p->next;
stack1.pop();
}
p->next=NULL;
return newHead;
}
int mian()
{
struct ListNode *head;
ReserveList(head);
return 0;
}
用栈实现单链表的逆转
最新推荐文章于 2022-08-18 17:53:40 发布