C语言 | Leetcode C语言题解之第142题环形链表II

题目:

题解:

cpp 复制代码
struct ListNode* detectCycle(struct ListNode* head) {
    struct ListNode *slow = head, *fast = head;
    while (fast != NULL) {
        slow = slow->next;
        if (fast->next == NULL) {
            return NULL;
        }
        fast = fast->next->next;
        if (fast == slow) {
            struct ListNode* ptr = head;
            while (ptr != slow) {
                ptr = ptr->next;
                slow = slow->next;
            }
            return ptr;
        }
    }
    return NULL;
}
相关推荐
AlenTech2 小时前
160. 相交链表 - 力扣(LeetCode)
数据结构·leetcode·链表
sin_hielo2 小时前
leetcode 1161(BFS)
数据结构·算法·leetcode
iAkuya4 小时前
(leetcode)力扣100 34合并K个升序链表(排序,分治合并,优先队列)
算法·leetcode·链表
放荡不羁的野指针5 小时前
leetcode150题-字符串
数据结构·算法·leetcode
橘颂TA5 小时前
【剑斩OFFER】算法的暴力美学——存在重复元素Ⅱ
算法·leetcode·哈希算法·散列表·结构与算法
cg50175 小时前
力扣数据库——组合两个表
sql·算法·leetcode
灵哎惹,凌沃敏5 小时前
FreeRTOS 任务上下文切换核心函数:xPortPendSVHandler详解
c语言·arm开发
2501_941798736 小时前
面向微服务分布式事务补偿与最终一致性的互联网系统高可用设计与多语言工程实践分享
leetcode·模拟退火算法
ada7_7 小时前
LeetCode(python)22.括号生成
开发语言·数据结构·python·算法·leetcode·职场和发展
喵了meme7 小时前
C语言实战练习
c语言·开发语言