链表ojoj

链接: li1在这里插入图片描述

struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode *fast,*slow;
    fast=head;
    slow=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
        if(fast==slow)
        break;
    }
    if(fast==NULL||fast->next==NULL)
    return NULL;
    struct ListNode*new=head;
    int ct=0;;
    while(new!=slow)
    {
        new=new->next;
        slow=slow->next;
        ct++;
    }
    
    return slow;
}

链接: li2
在这里插入图片描述

struct Node* copyRandomList(struct Node* head) {
	struct Node* cur=head;
    if(cur==NULL)
    return NULL;
    while(cur)
    {
        struct Node* copy=(struct Node* )malloc(sizeof(struct Node));
        copy->val=cur->val;
        struct Node* next=cur->next;
        cur->next=copy;
        copy->next=next;
        cur=next;
    }
    cur=head;
    while(cur)
    {
        struct Node* next=cur->next;
        if(cur->random==NULL)
        {
            next->random=NULL;
        }
        else
        {
        next->random=cur->random->next;
        
        }
        cur=next->next;
    }
    struct Node* newhead=head->next;
    cur=head;
    while(cur->next->next)
    {
        struct Node* cur2=cur->next;
        struct Node*next=cur2->next;
        cur2->next=next->next;
        cur->next=next;
        cur=next;
    }
    cur->next->next=NULL;
    cur->next=NULL;
    return newhead;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值