#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; }
时间: 2024-10-13 12:33:32