[LeetCode] 142. 环形链表 II+138. 复制带随机指针的链表

👉👉👉👉LeetCode刷题训练营👈👈👈👈

在这里插入图片描述

欢迎订阅本专栏,说在前面💖

今天给大家带来leetcode相关专栏习题的讲解!

博主为了本系列专栏,做了很多准备,争取图文并茂,让大家看明白!希望大家不要吝啬订阅,与关注,多多评论哦!!💖💖💖💖💖💖💖

在这里插入图片描述

一、前言

💪💪💪💪那么这里博主先安利一下其他一些干货满满的专栏啦!💪💪💪💪

玩转Linux操作系统+系统编程+网络编程,快速掌握数据结构宝藏文章,点击下方蓝字即可跳转:
🔝🔝🔝🔝🔝玩转Linux操作系统+系统编程+网络编程🔝🔝🔝🔝🔝

CCF相关真题,点击下方蓝字即可跳转:
🔝🔝🔝🔝🔝 CCF真题🔝🔝🔝🔝🔝

纵横数据结构与算法,点击下方蓝字跳转:
🔝🔝🔝🔝🔝数据结构与算法🔝🔝🔝🔝🔝

二 、正文

142. 环形链表 II

环形链表 II
在这里插入图片描述

在这里插入图片描述

struct ListNode *detectCycle(struct ListNode *head) {

    struct ListNode* fast =head;
    struct ListNode* slow =head;
    struct ListNode* meet =NULL;
    struct ListNode* head2 =head;

    while(fast && fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
       
        if(fast == slow)
        {
            meet = slow;
            break;
        }     

    }

    if(meet == NULL)
    {
        return NULL;
    }

    while(head2 != meet)
    {
        
        head2=head2->next;
        meet=meet->next;
    }
        return meet;
    
}

138. 复制带随机指针的链表

复制带随机指针的链表
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

struct Node* copyRandomList(struct Node* head) {
     struct Node* cur = head;
     while(cur)
    {
      struct Node* copy  = (struct Node*)malloc(sizeof(struct Node));
        copy->val = cur->val;
        copy->next = cur->next;

        cur->next = copy;
        cur = copy->next;

    }

    
    cur=head;

    while(cur)
    {
        struct Node* copy =cur->next;
        if(cur->random == NULL)
        {
            copy->random = NULL;
        }
        else{
        copy->random = cur->random->next;
        }
        cur = copy->next;
    }


    cur = head;
    struct Node* copyhead =NULL;
    
    struct Node* copytail =NULL;
    
   
    while(cur)
    {
        struct Node* copy =cur->next;
        struct Node* tail =copy->next;


        if(copyhead == NULL)
        {
            copyhead=copytail=copy;
        }
        else{
            copytail->next=copy;
            copytail=copytail->next;
        }
        cur->next = tail;
        cur=tail;
    }


	return copyhead;
	
}

三、 结尾💖

🌹🌹🌹🌹🌹🌹🌹 感谢大家的点赞关注,希望大家多多评论!!!🌹🌹🌹🌹🌹🌹🌹

在这里插入图片描述

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AF帆_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值