翻转链表
优质
小牛编辑
116浏览
2023-12-01
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead==nullptr||pHead->next==nullptr) return pHead;
ListNode *p=pHead->next;
ListNode *q=p->next;
pHead->next=nullptr;
while(p)
{
p->next=pHead;
pHead=p;
p=q;
if(q) q=q->next;
}
return pHead;
}
};