/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(!pHead) return pHead;
ListNode* newHead = new ListNode(0);
ListNode *p = pHead, *q = NULL, *t;
while(p) {
t = p->next;
p->next = q;
newHead->next = p;
q = p;
p = t;
}
return newHead->next;
}
};
【剑指offer】JZ15 反转链表 C++
最新推荐文章于 2022-07-17 00:51:03 发布