Linked List Cycle && Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note: Do not modify the linked list.

Follow up:
Can you solve it without using extra space?

Subscribe to see which companies asked this question

查看是否有环,快慢两个指针一个移动一步,一个移动两步,是否相遇

查看环的起点,相遇后,将其中一个指针移到链表头,两个指针每次向后移动一步,相遇的点就是环的起点

证明可参考:http://www.cnblogs.com/wuyuegb2312/p/3183214.html

Linked List Cycle

bool hasCycle(ListNode *head) {
    if (head == nullptr || head->next == nullptr)
        return false;
    ListNode *slow = head, *fast = head;
    while (fast->next && fast->next->next)
    {
        fast = fast->next->next;
        slow = slow->next;
        if (fast == slow)
            return true;
    }
    return false;
}

Linked List Cycle II

ListNode *detectCycle(ListNode *head) {
    if (head == nullptr || head->next == nullptr)
        return nullptr;
    ListNode *slow = head, *fast = head;
    while (fast->next && fast->next->next)
    {
        fast = fast->next->next;
        slow = slow->next;
        if (fast == slow)
        {
            slow = head;
            while (slow != fast)
            {
                slow = slow->next;
                fast = fast->next;
            }
            return slow;
        }
    }
    return nullptr;
}
时间: 2024-08-07 16:59:00

Linked List Cycle && Linked List Cycle II的相关文章

LeetCode解题报告:Linked List Cycle && Linked List Cycle II

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo

LeetCode之“链表”:Linked List Cycle && Linked List Cycle II

1. Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 刚看到这道题,很容易写出下边的程序: 1 bool hasCycle(ListNode *head) { 2 ListNode *a = head, *b = head; 3 while(a) 4 { 5 b =

leetcode. Linked List Cycle && Linked List Cycle ii

Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 使用双指针fast和slow,fast每次走两步,slow每次走一步,如果fast追上slow则存在环,否则不存在. 1 bool hasCycle(ListNode *head) 2 { 3 ListNode *slow = head, *fast = head; 4 whil

[Linked List]Linked List Cycle,Linked List Cycle II

一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? (M) Linked List Cycle II /** * Definition for singly-

LeetCode Linked List Cycle & Linked List Cycle II题解

Linked List Cycle Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space? 题目的意思就是在利用O(1)的空间判断一个链表是否存在环.我一开始的想法就是,每访问 一个节点,就遍历这个节点前面的所有节点,然后判断是否相同,如果相同就说明有环.如果所有的节点都访问完了还是没有发现有节点相同,则说明该链表没有环.但是这个想法很

LeetCode[Linked List]: Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

[Cycle.js] The Cycle.js principle: separating logic from effects

The guiding principle in Cycle.js is we want to separate logic from effects. This first part here was logical, and this second part here was effects. Effects are everything that change the external world somehow, the real world such as the DOM is con

[Linked List]Palindrome Linked List

Total Accepted: 29652 Total Submissions: 117516 Difficulty: Easy Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? (E) Palindrome Number (E) Valid Palindrome (E) Reverse Linked List /*

[Linked List]Remove Linked List Elements

Total Accepted: 43183 Total Submissions: 160460 Difficulty: Easy Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --&