反转链表
题目描述
输入一个链表,反转链表后,输出新链表的表头。
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } };*/ class Solution { public: ListNode* ReverseList(ListNode* pHead) { auto pre = pHead; auto back = pHead; while(back != nullptr && back->next != nullptr) { pre = back->next; back->next = pre->next; pre->next = pHead; pHead = pre; } return pHead; } };