LeetCode 206. 反转链表

目录

1.原题链接:

2.双指针:

代码实现: 

①.C语言版: 

②.C++版:

3.头插:

代码实现:

①.C语言版:

②.C++版:

4.递归:

从左往右递归:

代码实现:

①.C语言版: 

②.C++版:

从右往左递归:

代码实现:

①.C语言版:

②.C++版:

5.提交结果:

6.读书分享:


1.原题链接:

206. 反转链表

2.双指针:

我们可以用两个指针,一个指向第一个结点,一个指向第二个结点,然后让后一个结点指向前一个节点就好了,但是如果只使用两个指针会存在一个问题:当让后一个结点指向前一个结点的时候,后面的结点会找不到。所以这里需要三个指针方便后续操作。并且最开始要让最左边指针指向NULL,因为反转后的链表最后一个结点后面也会指向NULL。

代码实现: 

①.C语言版: 

struct ListNode* reverseList(struct ListNode* head) {
    typedef struct ListNode ListNode;//重命名
    ListNode* left = NULL;
	ListNode* mid = head;
    ListNode* right;//防止找不到后面的结点
	while (mid!=NULL)
	{
		right = mid->next;
		mid->next = left;
		left = mid;
		mid = right;
        // right=right->next;//如果这样写会存在对空指针的访问
	}
	return left;
}

②.C++版:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* left = NULL;
        ListNode* mid = head;
        ListNode* right;//防止找不到后面的结点
        while (mid!=NULL)
        {
            right = mid->next;
            mid->next = left;
            left = mid;
            mid = right;
            // right=right->next;//如果这样写会存在对空指针的访问
        }
        return left;
    }
};

3.头插:

代码实现:

①.C语言版:

struct ListNode* reverseList(struct ListNode* head) {
    typedef struct ListNode ListNode; 
    ListNode* cur=head;
    ListNode* newHead=NULL;
    while(cur!=NULL)
    {
        ListNode* next=cur->next;//放在里面不会引起访问空指针的问题
        cur->next=newHead;
        newHead=cur;
        cur=next;
    }
    return newHead;
}
②.C++版:
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* cur=head;
        ListNode* newHead=NULL;
        while(cur!=NULL)
        {
            ListNode* next=cur->next;//放在里面不会引起访问空指针的问题
            cur->next=newHead;
            newHead=cur;
            cur=next;
        }
        return newHead;   
    }
};
    

4.递归:

从左往右递归:

此方法跟三个指针的方法相似,应该会稍微好理解些。

开始进入递归函数:

 完成第一次反转:

  递归反转:

代码实现:

①.C语言版: 
struct ListNode* reverse(struct ListNode* pre,struct ListNode* cur)
{
    if(cur==NULL)//递归出口
        return pre;
    struct ListNode* late=cur->next;
    cur->next=pre;
    return reverse(cur,late);
}
struct ListNode* reverseList(struct ListNode* head){
    return reverse(NULL,head);
}
②.C++版:
class Solution {
public:
    ListNode* reverse(ListNode* pre,ListNode* cur)
    {
        if(cur==NULL)//递归出口
            return pre;
        ListNode* late=cur->next;
        cur->next=pre;
        return reverse(cur,late);
    }
    ListNode* reverseList(ListNode* head) {
        return reverse(NULL,head);
    }
};

从右往左递归:

先递归到最后一个结点后,回来时再反转。

代码实现:

①.C语言版:
struct ListNode* reverseList(struct ListNode* head){
    typedef struct ListNode ListNode;
    if(head==NULL||head->next==NULL)//递归结束条件
            return head;
        ListNode* newHead=reverseList(head->next);//开始进入
        head->next->next=head;
        head->next=NULL;
        return newHead;
}
②.C++版:
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head==NULL||head->next==NULL)//递归结束条件
            return head;
        ListNode* newHead=reverseList(head->next);//开始进入
        head->next->next=head;
        head->next=NULL;
        return newHead;
    }
};

5.提交结果:

6.读书分享:

道德经·第三十七章》:

不欲以静,天下将自定。

解释:不起贪欲而趋于平静,天下便自然复归于安定。

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值