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;
}
相关推荐
文丑颜不良啊41 分钟前
LeetCode2241~2260题目解析
java·leetcode
文丑颜不良啊1 小时前
LeetCode2321~2340题目解析
java·leetcode
wuyk55512 小时前
Keil工具链-第05讲-启动文件与__main到底做了什么
c语言·stm32·单片机·嵌入式硬件·物联网
文丑颜不良啊20 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z20 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
冻柠檬飞冰走茶21 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊1 天前
LeetCode2161~2180题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2141~2160题目解析
java·leetcode
Hhy_11071 天前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
曦樂~2 天前
Cpp面筋
c语言·c++