剑指offer--22.反转链表
时间限制:1秒 空间限制:32768K 热度指数:440624
题目描述
输入一个链表,反转链表后,输出新链表的表头。
/* 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* node = NULL,*pNext; pNext = pHead->next;//reserve pHead->next = NULL; //断开 node = pHead;//start pHead = pNext; while(pHead != NULL) { pNext = pHead->next;//reserve pHead->next = NULL; //断开 pHead->next = node;//反转 node = pHead; pHead = pNext;//接着 } return node; } };