输入一个链表,反转链表后,输出新链表的表头。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* h=NULL;//新链表的表头
while(pHead!=NULL)
{
ListNode* htemp=pHead->next;
pHead->next=h;//断掉原来pHead指向pHead.next的链,重新定义pHead指向h的新链
h=pHead;//h向前移一位 新链表的表头是h
pHead=htemp;//pHead取代原链表的phead.next位置
}
return h;
}
};