数据结构之链表OJ题(下)

141. 环形链表 - 力扣(LeetCode)https://leetcode.cn/problems/linked-list-cycle/

首先一个链表要是是环状,那么他一直走也不会走到自己是空的情况

如果一个链表不是环状,那他走到最后一定会走到自己是空的情况

接下来用快慢指针的思维开始运行,让快指针每次都比慢指针多走一步,又换的情况下最终快指针都会赶上套住慢指针

cpp 复制代码
bool hasCycle(struct ListNode *head) {
    struct ListNode* slow=head;
    struct ListNode *fast=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
        if(fast==slow)
        {
            return true;
        }

    }
    return false;
}

142. 环形链表 II - 力扣(LeetCode)https://leetcode.cn/problems/linked-list-cycle-ii/description/

这道题就是结合了判断有环和两个链表找是否用重合的办法

cpp 复制代码
struct ListNode *detectCycle(struct ListNode *head) {
    if (head == NULL)
        return NULL;
    struct ListNode* slow=head;
    struct ListNode *fast=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
        if(fast==slow)
        {
            struct ListNode *curA=head;
            struct ListNode *curB=fast->next;
            struct ListNode *curC=fast->next;

            fast->next = NULL;
            
            int lenA=0,lenB=0;

            while(curA)
            {
                curA=curA->next;
                lenA++;
            }
             while(curB)
            {
                curB=curB->next;
                lenB++;
            }
            curA=head;
            curB=curC;
            int gap=0;
            if(lenA>=lenB)
            {
                gap=lenA-lenB;
                while(gap--)
                {
                    curA=curA->next;

                }
                while(curA!=curB)
                {
              
                    curA=curA->next;
                    curB=curB->next;
                }
                return curA;
            }
            else
            {
                 gap=lenB-lenA;
                while(gap--)
                {  
                    curB=curB->next;

                }
                while(curA!=curB)
                {

                    curA=curA->next;
                    curB=curB->next;
                }
                return curA;
            }
        }

    }
    return NULL;

}

LCR 026. 重排链表 - 力扣(LeetCode)https://leetcode.cn/problems/LGjMqU/

这道题融合了之前逆置和找中间节点的思路

先要完成找中间节点,然后将后半部分链表进行逆置,再将其一个一个插入即可.

在做题时我们要注意边界情况就是链表内节点个数时偶数和奇数会不会对这段解题思路有影响,在这道题中是没有影响的

cpp 复制代码
void reorderList(struct ListNode* head){
    //利用快慢指针找到中间节点

    struct ListNode* slow=head;
    struct ListNode *fast=head;
    while(fast&&fast->next)
    {
        slow=slow->next;
        fast=fast->next->next;
    }
    //slow为中间节点
    struct ListNode *cur=slow->next;
    slow->next=NULL;

    //将后半段链表逆置
    struct ListNode *newmid=NULL;
    while(cur)
    {
    //采用头插来实现逆置
    struct ListNode *next=cur->next;
    cur->next=newmid;
    newmid=cur;
    cur=next;
    }
    struct ListNode* insertnode=head;
    cur=newmid;
    //再将其隔一个进行插入
    while(cur)
    {
        struct ListNode *next=cur->next;
        struct ListNode *insertnext=insertnode->next;

        cur->next=insertnode->next;
        insertnode->next=cur;
        cur=next;
        insertnode=insertnext;

    }

}
相关推荐
祖力554 小时前
数据结构的基本概念与单向链表(链表数据类型构造、创建、头插、尾插、头删、尾删)
数据结构·链表
白狐_7987 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
zander2587 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法
船厂电气自动化ai大模型8 小时前
AI大模型与数学 第32课 函数凹凸性与二阶导数:拐点求解、凹凸区间计算(10道二阶导数计算题)
数据结构·人工智能·python·深度学习·算法
旖旎夜光8 小时前
LeetCode 3:无重复字符的最长子串(滑动窗口) —— 题解
数据结构·c++·算法·leetcode·滑动窗口
rannn_11111 小时前
【力扣hot100】链表专题下|138、148、23、146
java·算法·leetcode·链表·开发
不会就选b12 小时前
算法日常・每日刷题--<优先级队列>2
数据结构·算法
LuminousCPP13 小时前
单链表专题(三)-刷题复盘篇:从快慢指针到环形链表 II 数学推导
数据结构·经验分享·笔记·学习·算法·链表
ChaoZiLL13 小时前
二叉树经典例题
数据结构·算法
三克的油14 小时前
算法基础-1
数据结构·c++·算法