题目
输入一个链表,反转链表后,输出新链表的表头。
代码
头插法
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead==NULL)
return NULL;
ListNode *p=pHead,*q;
pHead=NULL;
while(p){
q=p->next;
p->next=pHead;
pHead=p;
p=q;
}
return pHead;
}
};