漫画 网站 源码,后台网站模板 html,官网设计模板,wordpress pdf生成LeetCode206,直接看下面链接的动画演示部分就行了,就能快速理解pre和cur的作用 代码随想录:反转链表 这个是另外的在B站的视频讲解链接 【帮你拿下反转链表 | LeetCode#xff1a;206.反转链表 | 双指针法 | 递归法】 https://www.bilibili.com/video/BV1nB4y1i7eL/?share_so…LeetCode206,直接看下面链接的动画演示部分就行了,就能快速理解pre和cur的作用 代码随想录:反转链表 这个是另外的在B站的视频讲解链接 【帮你拿下反转链表 | LeetCode206.反转链表 | 双指针法 | 递归法】 https://www.bilibili.com/video/BV1nB4y1i7eL/?share_sourcecopy_webvd_sourceafbacdc02063c57e7a2ef256a4db9d2a
class Solution {
public:ListNode* reverseList(ListNode* head) {ListNode* prenullptr,*curnullptr,*tmpnullptr;curhead;while(cur!nullptr){tmpcur-next;cur-nextpre;precur;curtmp;}return pre;}
};递归方法: 具体运行过程是在倒数第二个节点反转倒数第一个节点的next指针指向,然后返回最后一个节点的指针,注意,这个递归每次返回的都是倒数第一个的节点的指针,否则这题无法使用递归完成.
class Solution {
public:ListNode* reverseList(ListNode* head) {if(headnullptr||head-nextnullptr){return head;}ListNode* newHeadreverseList(head-next);head-next-nexthead;head-nextnullptr;return newHead;}
};