题目描述
输入一个链表,反转链表后,输出新链表的表头。
示例1
输入
{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* tmp=NULL;
while(pHead)
{
tmp=pHead->next;
pHead->next=pre;
pre=pHead;
pHead=tmp;
}
return pre;
}
};