/*
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:反转链表
最新推荐文章于 2022-06-07 07:36:04 发布