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

    }

}
相关推荐
CSharp精选营4 天前
关系型 vs 非关系型:从原理到选型,一文搞定数据库核心分类
数据结构·nosql·关系型数据库·非关系型数据库·技术选型
刘马想放假7 天前
Modbus 全栈技术解析:TCP、RTU、ASCII、RTU over TCP
数据结构·网络协议
北域码匠8 天前
冒泡排序太慢?鸡尾酒排序双向优化,原生 C# 零第三方库完整代码
数据结构·排序算法·泛型·c# 算法·鸡尾酒排序·原生 c# 开发·冒泡排序优化·嵌入式算法
Darling噜啦啦15 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
小小工匠16 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化
玖玥拾16 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
Qres82116 天前
算法复键——树状数组
数据结构·算法
牛油果子哥q16 天前
并查集(DSU)超精讲,路径压缩、按秩合并、万能模板、连通性判定、最小生成树与刷题实战全解
数据结构·c++·最小生成树·并查集
凌波粒16 天前
LeetCode--491.递增子序列(回溯算法)
数据结构·算法·leetcode
疯狂成瘾者16 天前
Java 集合 LinkedList 详解:链表结构、常用方法和队列使用
java·开发语言·链表