【数据结构】链表相关题目(2)

本文详细介绍了如何使用快慢指针检测环形链表的入口点,并提供了两种方法;同时讲解了随机链表的复制策略,包括在原链表每个节点后添加复制节点并调整random指针。
摘要由CSDN通过智能技术生成

目录

一、环形链表||

题解1

题解2

二、随机链表的复制

题解


一、环形链表||

题解1

        大体思路为先用快慢指针找相遇点,找到相遇点后用一个指针从链表头走,一个指针从相遇点走,当两指针相遇时,就是环的入口点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) 
{   
    struct ListNode* fast = head;
    struct ListNode* slow = head;
    
    while(fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if(fast == slow)
        {
            struct ListNode* phead = head;
            struct ListNode* plist = fast;
            while(plist != phead)
            {
                phead = phead->next;
                plist = plist->next;
            }
            return plist;
        }
    }
    return NULL;
}

题解2

还是先用快慢指针,找到相遇点,存储下此点的下一个点作为头节点,然后让此点指向空作为尾节点,后用链表相交的方法即可。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) 
{    
    struct ListNode* fast = head;
    struct ListNode* slow = head;    

    while(fast && fast->next)
    {
        fast = fast->next->next;
        slow = slow->next;

        if(fast == slow)
        {
            struct ListNode* meet = slow;
            struct ListNode* meetnext = slow->next;
            meet->next = NULL;
            struct ListNode* phead = head;
            struct ListNode* meetlist = meetnext;
            int num1 = 0;
            int num2 = 0;

            while(phead)
            {
                phead = phead->next;
                num1++;
            }

            while(meetlist)
            {
                meetlist = meetlist->next;
                num2++;
            }

            int sub = abs(num1-num2);
            struct ListNode* largelist = head;
            struct ListNode* lesslist = meetnext;

            if(num1 < num2)
            {
                largelist = meetnext;
                lesslist = head;
            }

            while(sub--)
            {
                largelist = largelist->next;
            }

            while(largelist)
            {
                if(largelist == lesslist)
                {
                    return largelist;
                }
                largelist = largelist->next;
                lesslist = lesslist->next;
            }
        }
    }
    return NULL;
}

二、随机链表的复制

题解

思路是先在原链表每个节点后面复制一个相同的节点,这样复制节点的random就是原节点random-->next,再断开复制节点即可。

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     struct Node *next;
 *     struct Node *random;
 * };
 */

struct Node* copyRandomList(struct Node* head) 
{
    struct Node* phead = head;
    while(phead)
    {
        struct Node* behind = (struct Node*)malloc(sizeof(struct Node));
        behind->val = phead->val;
        behind->next = phead->next;
        phead->next = behind;
        phead = phead->next->next;
    }

    phead = head;
    while(phead)
    {
        struct Node* behind = phead->next;
        if(phead->random == NULL)
        {
            behind->random = NULL;
        }
        else
        {
            behind->random = phead->random->next;
        }
        
        phead = phead->next->next;
    }

    phead = head;
    struct Node* behind = NULL;
    struct Node* ch = NULL;
    while(phead && phead->next)
    {       
        if(behind == NULL)
        {
            ch = behind = phead->next;
        }
        else
        {
            phead->next = phead->next->next;
            behind->next = behind->next->next;
        }

        phead = phead->next;
        behind = behind->next;
    }
    return ch;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值