/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead==NULL)
return 0;
ListNode *s=new ListNode(0);
ListNode* p=pHead->next;
ListNode* q=p;
pHead->next=NULL;
s->next=pHead;
while(q){
p=q;
q=p->next;
p->next=s->next;
s->next=p;
}
return s->next;
}
};
剑指offer:反转链表
最新推荐文章于 2023-11-13 11:28:18 发布