链表中环的检测

August 27, 2020
链表 c
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    if (head == NULL || head->next == NULL) return false;
    
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    while (fast->next != NULL && fast->next->next != NULL)
    {
        if (slow->next->val == fast->next->next->val) return true;
        
        slow = slow->next;
        fast = fast->next->next;
    }
    return false;
}

参考:
Linked List Cycle
leetcode - 141. Linked List Cycle

移除链表倒数第N个节点

链表 c

两个有序链表的合并

链表 c

反转单向链表

链表 c