题目描述
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
思路:三个指针边移边换指
C++代码
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pre=nullptr;
ListNode* node=head;
if(head==nullptr)
return head;
ListNode* nex=head->next;
while(node!=nullptr)
{
node->next=pre;
pre=node;
node=nex;
if(node!=nullptr)
nex=nex->next;
}
return pre;
}
};