题目描述
输入一个链表,反转链表后,输出新链表的表头。
AC代码
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(!pHead || !pHead->next)
return pHead;
ListNode *pre = NULL, *curNode = pHead;
while(curNode)
{
ListNode* Next = curNode->next;
curNode->next = pre;
pre = curNode;
curNode = Next;
}
return pre;
}
};