时间限制:1秒 空间限制:32768K 本题知识点: 链表 题目描述 输入一个链表,反转链表后,输出新链表的表头。
代码:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(!pHead){
return nullptr;
}
ListNode* q = pHead->next;
ListNode* r;
pHead->next = nullptr;
while(q != nullptr){
r = q->next;
q->next = pHead;
pHead = q;
q = r;
}
return pHead;
}
};