输入一个链表,反转链表后,输出反转链表后头节点
/*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;
if(pHead->next==NULL)
return pHead;
if(pHead->next->next==NULL){
pHead->next->next=pHead;
pHead->next=NULL;
}
ListNode* prev=pHead;
ListNode* temp=pHead->next;
pHead=pHead->next->next;
prev->next=NULL;
while(pHead!=NULL){
temp->next=prev;
prev=temp;
temp=pHead;
pHead=pHead->next;
}
temp->next=prev;
return temp;
}
};