当前位置:网站首页>Leetcode206. Reverse linked list

Leetcode206. Reverse linked list

2022-07-07 22:39:00 Small size big ha

Title Description : Here's the head node of the list head , Please reverse the list , And return the inverted linked list .

 Insert picture description here
 Insert picture description here

Ideas : Ideas : Reverse the pointer .

That is, take a null pointer pre
loop
Take a pointer next by cur Next node of
cur Next node of Change it to pre
pre Change it to cur
Give Way cur Next node
return pre

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre=nullptr;
        ListNode* cur=head;
        while(cur!=nullptr)
        {
            ListNode* next=cur->next;
            cur->next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }
};
原网站

版权声明
本文为[Small size big ha]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130603561572.html