《剑指Offer》-反转链表
反转链表
http://www.nowcoder.com/questionTerminal/75e878df47f24fdc9dc3e400ec6058ca
小白一枚,记录下自己的解题方法,日后回头看看自己有多菜
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if (!pHead)
return nullptr;
ListNode* pre = pHead; ListNode* cur = pre->next;
pHead = nullptr; pre->next = pHead;
while (cur != nullptr)
{
ListNode* tmp = cur->next;
cur->next = pre;
pre = cur; cur = tmp;
}
return pre;
}
};实例图示:
