数据结构之链表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;

    }

}
相关推荐
青山木3 小时前
Hot 100 --- 最长有效括号
java·数据结构·算法·leetcode·动态规划
爱吃苹果的日记本5 小时前
数据结构第五课
数据结构·学习
Logic1016 小时前
C语言/数据结构贪心算法题解:买卖股票的最佳时机——一次交易最大利润(O(n)时间O(1)空间)
c语言·数据结构·贪心算法·数组·时间复杂度·算法题·股票交易
Logic1016 小时前
C语言/数据结构位运算题解:异或XOR找出货币交易中的“独特面值“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
ctlover6 小时前
数据结构:树
数据结构·python
景熙55238 小时前
14.Java 集合框架从入门到源码万字详解(含 ArrayList/LinkedList/HashMap 源码、泛型通配符、红黑树)
java·开发语言·数据结构·算法
SendTomo9 小时前
.ico透明图标在线生成下载平台深度解析
大数据·数据结构·数据库·数据仓库·个人开发
GG-_-Bond9 小时前
9.1kv存储持久化模拟面试
linux·c语言·数据结构·c++
程序员阿鹏9 小时前
双亲委派机制
java·jvm·数据结构·后端
runningshark9 小时前
数据结构-第二章-线性表及其顺序存储
数据结构