单链表Ⅲ(LeetCode)

个人主页流年如梦

专栏《C语言》 《数据结构》

文章目录

一.相交链表

点击转跳👈

原题

🧐思路 :这道题要找两个链表的相交节点,核心方法是双指针拼接法;先用两个指针分别遍历两个链表,当一个链表遍历完后,就跳到另一个链表的头部继续遍历;这两个指针最终会同时到达相交节点(如果相交),或者同时到达NULL(如果不相交)

参考代码如下

c 复制代码
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *pA = headA, *pB = headB;
    while (pA != pB) {
        pA = pA ? pA->next : headB;
        pB = pB ? pB->next : headA;
    }
    return pA;
}

二.环形链表Ⅰ

点击转跳👈

原题

🧐思路 :这道题是判断链表是否有环,核心用快慢指针法;快指针每次走两步,慢指针每次走一步;如果链表有环,快指针一定会在环内追上慢指针;如果没环,快指针会先走到链表末尾

参考代码如下

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

三.环形链表Ⅱ

点击转跳👈

原题

🧐思路 :这道题的核心思路也是老方法快慢指针法;先用快慢指针判断链表是否有环(快指针走两步,慢指针走一步,相遇则有环);当相遇后,让一个指针回到链表头,两个指针同时以相同速度前进,再次相遇的节点就是入环点

参考代码如下

c 复制代码
struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode *slow = head, *fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            struct ListNode *p = head;
            while (p != slow) {
                p = p->next;
                slow = slow->next;
            }
            return p;
        }
    }
    return NULL;
}

四.随机链表的复制

点击转跳👈

原题

🧐思路 :这道题的核心思路是在原节点后插入复制节点 + 拆分链表 ;先遍历原链表,在每个节点后面插入它的复制节点;再遍历链表,给每个复制节点设置random指针(copy->random = old->random ? old->random->next : NULL);把链表拆分成原链表和复制链表两部分,返回复制链表的头节点

参考代码如下

c 复制代码
struct Node* copyRandomList(struct Node* head) {
    if (head == NULL) return NULL;
    struct Node* cur = head;
    while (cur) {
        struct Node* copy = (struct Node*)malloc(sizeof(struct Node));
        copy->val = cur->val;
        copy->next = cur->next;
        cur->next = copy;
        cur = copy->next;
    }
    cur = head;
    while (cur) {
        struct Node* copy = cur->next;
        copy->random = cur->random ? cur->random->next : NULL;
        cur = copy->next;
    }
    cur = head;
    struct Node* copyHead = head->next;
    while (cur) {
        struct Node* copy = cur->next;
        cur->next = copy->next;
        cur = cur->next;
        if (cur) {
            copy->next = cur->next;
        }
    }
    return copyHead;
}

🎯总结

这四道链表题,核心都是用双指针解决链表问题

  1. 复制随机链表 --> 通过在原节点后插入副本,再拆分链表实现深拷贝
  2. 环形链表判断 --> 快慢指针追逐法,相遇即说明有环
  3. 环形链表入环点 --> 快慢指针相遇后,再用同速指针定位入环点
  4. 相交链表 --> 双指针交叉遍历,相遇点就是相交节点

👀 关注 我们一路同行,从入门到大师,慢慢沉淀、稳步成长
❤️ 点赞 鼓励原创,让优质内容被更多人看见
⭐ 收藏 收好核心知识点与实战技巧,需要时随时查阅
💬 评论 分享你的疑问或踩坑经历,一起交流避坑、共同进步

相关推荐
aaaameliaaa6 小时前
字符函数和字符串函数
c语言·笔记·算法
城管不管7 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
月疯8 小时前
二分法算法(水平等分图形面积)
算法
豆瓣鸡8 小时前
算法日记 - Day3
java·开发语言·算法
白白白小纯8 小时前
算法篇—反转链表
c语言·数据结构·算法·leetcode
Achou.Wang9 小时前
深入理解go语言-第5章 并发编程——Go的灵魂
大数据·算法·golang
The Chosen One9859 小时前
高进度算法模板速记(待完善)
java·前端·算法
圣保罗的大教堂11 小时前
leetcode 3517. 最小回文排列 I 中等
leetcode
土豆.exe11 小时前
Fastjson2 2.0.53 哈希碰撞 RCE:从原理到三种打法
算法·哈希算法
黄河123长江11 小时前
有限Abel群的结构()
算法