友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

141. 环形链表

给你一个链表的头节点 head ,判断链表中是否有环。

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

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

示例 1:

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

示例 2:

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

示例 3:

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

提示:

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

  • -105<= Node.val <= 105

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

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

思路分析

除了双指针外,还可以使用 Map 来解决。只是空间复杂度要高一些。

0141 04
0141 05
0141 06
0141 07
0141 08
  • 一刷

  • 二刷

  • 三刷

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 * Runtime: 0 ms, faster than 100.00% of Java online submissions for Linked List Cycle.
 *
 * Memory Usage: 37.9 MB, less than 84.29% of Java online submissions for Linked List Cycle.
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-01-12 10:06
 */
public boolean hasCycle(ListNode head) {
    if (Objects.isNull(head)) {
        return false;
    }
    ListNode slow = head;
    ListNode fast = head.next;
    while (Objects.nonNull(fast)) {
        if (slow == fast) {
            return true;
        }
        slow = slow.next;
        fast = fast.next;
        if (Objects.nonNull(fast)) {
            fast = fast.next;
        }
    }
    return false;
}

/**
 * Runtime: 5 ms, faster than 6.60% of Java online submissions for Linked List Cycle.
 *
 * Memory Usage: 38.3 MB, less than 82.14% of Java online submissions for Linked List Cycle.
 *
 * Copy from: https://leetcode.com/problems/linked-list-cycle/solution/[Linked List Cycle solution - LeetCode]
 */
public boolean hasCycleMap(ListNode head) {
    Set<ListNode> nodes = new HashSet<>();
    ListNode current = head;
    while (Objects.nonNull(current)) {
        if (nodes.contains(current)) {
            return true;
        } else {
            nodes.add(current);
        }
        current = current.next;
    }
    return false;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * 快慢指针
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2024-07-02 21:00:01
 */
public boolean hasCycle(ListNode head) {
  if (head == null || head.next == null) {
    return false;
  }
  ListNode slow = head;
  ListNode fast = head;
  while (fast != null && fast.next != null) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow == fast) {
      return true;
    }
  }
  return false;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/**
 * @author D瓜哥 · https://www.diguage.com
 * @since 2025-03-06 21:23:36
 */
public boolean hasCycle(ListNode head) {
  ListNode slow = head, fast = head;
  while (fast != null && fast.next != null) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow == fast) {
      return true;
    }
  }
  return false;
}