141. 环形链表(Linked List Cycle)E

英文题目

  • Given head, the head of a linked list, determine if the linked list has a cycle in it.

  • There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.

  • Return true if there is a cycle in the linked list. Otherwise, return false.

  • Example 1:

  • Input: head = [3,2,0,-4], pos = 1
    Output: true
    Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
    
  • Example 2:

  • Input: head = [1,2], pos = 0
    Output: true
    Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
    
  • Example 3:

  • Input: head = [1], pos = -1
    Output: false
    Explanation: There is no cycle in the linked list.
    
  • Constraints:

  • The number of the nodes in the list is in the range [0, 10^4].

  • -10^5 <= Node.val <= 10^5

  • pos is -1 or a valid index in the linked-list.

  • Follow up: Can you solve it using O(1) (i.e. constant) memory?

中文题目

  • 给定一个链表,判断链表中是否有环。

  • 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

  • 如果链表中存在环,则返回 true 。 否则,返回 false 。

  • 进阶:

  • 你能用 O(1)(即,常量)内存解决此问题吗?

  • 示例 1:

  • 输入:head = [3,2,0,-4], pos = 1
    输出:true
    解释:链表中有一个环,其尾部连接到第二个节点。
    
  • 示例 2:

  • 输入:head = [1,2], pos = 0
    输出:true
    解释:链表中有一个环,其尾部连接到第一个节点。
    
  • 示例 3:

  • 输入:head = [1], pos = -1
    输出:false
    解释:链表中没有环。
    
  • 提示:

  • 链表中节点的数目范围是 [0, 10^4]

  • -10^5 <= Node.val <= 10^5

  • pos 为 -1 或者链表中的一个 有效索引

哈希表

  • 把遍历过的节点存到哈希表中,遍历新节点时,如果哈希表中已存在,则说明有环,否则无环

  • 时间复杂度O(N),空间复杂度O(N)

    #执行用时:64 ms, 在所有 Python3 提交中击败了64.11%的用户
    #内存消耗:18.6 MB, 在所有 Python3 提交中击败了8.89%的用户
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution:
        def hasCycle(self, head: ListNode) -> bool:
            seen = set()
            while head:
                if head in seen:
                    return True
                seen.add(head)
                head = head.next
            return False
    
    // c++: 时间 12 ms, 击败 52.77%; 内存 10.3 MB, 击败 12.63%
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            unordered_set<ListNode *> seen;
            while (head != nullptr) {
                if (seen.find(head) != seen.end()) {
                    return true;
                }
                seen.insert(head);
                head = head->next;
            }
            return false;
        }
    };
    
    // java: 时间 4 ms, 击败 12.68%; 内存 42.1 MB, 击败 93.10%
    /**
     * Definition for singly-linked list.
     * class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public boolean hasCycle(ListNode head) {
            Set<ListNode> seen = new HashSet<>();
            while (head != null) {
                if (seen.contains(head)) {
                    return true;
                }
                seen.add(head);
                head = head.next;
            }
            return false;
        }
    }
    
    // go: 时间 12 ms, 击败 15.72%; 内存 6.1 MB, 击败 5.9%
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func hasCycle(head *ListNode) bool {
        seen := map[*ListNode]bool{}
        for head != nil {
            if _, ok := seen[head]; ok {
                return true
            }
            seen[head] = true
            head = head.Next
        }
        return false
    }
    
    // javascript: 时间 72 ms, 击败 73.57%; 内存 44.6 MB, 击败 9.59%
    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    
    /**
     * @param {ListNode} head
     * @return {boolean}
     */
    var hasCycle = function(head) {
        const seen = new Set();
        while (head != null) {
            if (seen.has(head)) {
                return true;
            }
            seen.add(head);
            head = head.next;
        }
        return false;
    };
    

快慢指针

  • 快指针每次走两步,慢指针每次走一步,如果两个能相遇即有环

  • 时间复杂度O(N),N是链表中的节点

  • 空间复杂度O(1)

    # 执行用时:56 ms, 在所有 Python3 提交中击败了85.62%的用户
    # 内存消耗:16.5 MB, 在所有 Python3 提交中击败了27.28%的用户
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution:
        def hasCycle(self, head: ListNode) -> bool:
            fast = slow = head
            # 判断条件这么写可以不预先判断 head 指针是否存在
            while fast and fast.next:
                slow, fast = slow.next, fast.next.next
                if fast == slow:
                    return True
            return False
    
    // c++: 时间 8 ms, 击败 92.34%; 内存 7.8 MB, 击败 77.60%
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            ListNode *fast = head, *slow = head;
            while (fast && fast->next) {
                slow = slow->next;
                fast = fast->next->next;
                if (fast == slow) {
                    return true;
                }
            }
            return false;
        }
    };
    
    // java: 时间 0 ms, 击败 100%; 内存 42.4 MB, 击败 59.73%
    /**
     * Definition for singly-linked list.
     * class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public boolean hasCycle(ListNode head) {
            ListNode fast = head, slow = head;
            while (fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next.next;
                if (fast == slow) {
                    return true;
                }
            }
            return false;
        }
    }
    
    // go: 时间 8 ms, 击败 71.77%; 内存 4.2 MB, 击败 100%
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func hasCycle(head *ListNode) bool {
        fast, slow := head, head
        for fast != nil && fast.Next != nil {
            slow = slow.Next
            fast = fast.Next.Next
            if fast == slow {
                return true
            }
        }
        return false
    }
    
    // javascript: 时间 76 ms, 击败 54.30%; 内存 43.9 MB, 击败 29.36%
    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    
    /**
     * @param {ListNode} head
     * @return {boolean}
     */
    var hasCycle = function(head) {
        let fast = head, slow = head;
        while (fast && fast.next) {
            slow = slow.next;
            fast = fast.next.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    };
    
Last Updated:
Contributors: Shiqi Lu