其他分享
首页 > 其他分享> > LeetCode刷题笔记- 143.翻转链表

LeetCode刷题笔记- 143.翻转链表

作者:互联网

LeetCode刷题笔记- 141.环形链表

不同速率同赛道相遇方案

C代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    struct ListNode *tf = NULL; 
    struct ListNode *ts = NULL;

    if (head == NULL)
        return false;
    tf = head;
    ts = head->next;

    while (1) {
        if (tf == NULL)
            break;
        if (ts == NULL)
            break;
        if (tf == ts)
            return true;
        tf = tf->next;
        ts = ts->next;
        if (ts == NULL)
            break;
        ts = ts->next;
    }
    return false;
}

注意点

  1. 快者与慢者的起跑点初始化需要不同更方便实现;

标签:ListNode,143,struct,ts,next,链表,tf,NULL,刷题
来源: https://blog.csdn.net/sz66cm/article/details/123594315