反转链表 -D
题目描述 : ------------------------------------------------------------
输入一个链表,反转链表后,输出新链表的表头。
输入样例 : ------------------------------------------------------------
{1,2,3}
返回值 : ------------------------------------------------------------
{3,2,1}
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* pre = NULL;
ListNode* next = pHead;
while (next) {
next = pHead->next;
pHead->next = pre;
pre = pHead;
if (next == NULL)
return pHead;
pHead = next;
}
return pHead;
}
};