头图

Niuke.com High-Frequency Algorithm Question Series-BM6-Determine whether there is a cycle in the linked list

Niuke.com High-Frequency Algorithm Question Series-BM6-Determine whether there is a cycle in the linked list

Topic description

Checks whether there is a cycle in the given linked list. Returns true if there is a loop, false otherwise.

See the original title: BM6 determines whether there is a cycle in the linked list

Solution 1: Double pointer method

Use two pointers, fast and slow. They all start at the head of the linked list. Subsequently, the slow pointer moves backward one position at a time, while the fast pointer moves backward two positions. If there is a ring in the linked list, the fast pointer will eventually meet the slow pointer again in the ring.

For the principle, please refer to: Detailed explanation of the principle of double pointer algorithm

Solution 2: Hash method

Use HashSet to record the nodes in the linked list, and then traverse the linked list nodes:

  • If the node in the linked list has appeared in the hash table, it means that the linked list has a ring, and returns true directly
  • If the node in the linked list has not appeared in the hash table, add the current node to the hash table, and then judge the next node

Finally, if there are no duplicate nodes, it means there is no cycle and returns false.

code

 import java.util.HashSet;

public class Bm006 {
    /**
     * 双指针
     *
     * @param head
     * @return
     */
    public static boolean hasCycle(ListNode head) {
        ListNode fast = head, slow = head;
        while (fast != null && fast.next != null) {
            // 快指针每次走2步,慢指针每次走1步
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                // 快慢指针相遇,说明链表中有环
                return true;
            }
        }
        // 快慢指针没有相遇,说明无环
        return false;
    }

    /**
     * 哈希法
     *
     * @param head
     * @return
     */
    public static boolean hasCycle2(ListNode head) {
        // 用来记录链表中未重复的结点
        HashSet<ListNode> nodes = new HashSet<>();
        while (head != null) {
            // 如果链表中的结点已经出现过,说明有环,返回true
            if (nodes.contains(head)) {
                return true;
            }
            nodes.add(head);
            head = head.next;
        }
        // 如果没有重复节点,则说明无环,返回false。
        return false;
    }

    public static void main(String[] args) {
        /**
         * 测试用例链表结构为有环
         * testCaseCycle:  3 -> 2 -> 0 -> -4
         *                      ^          |
         *                      ------------
         */
        ListNode head = ListNode.testCaseCycle();
        /**
         * 测试用例,期望输出: true
         */
        System.out.println(hasCycle(head));
        System.out.println(hasCycle2(head));
    }
}
$1.01^{365} ≈ 37.7834343329$
$0.99^{365} ≈ 0.02551796445$
Believe in the power of persistence!

LeetCodet题解
技术学习

玉树临风,仙姿佚貌!

1.8k 声望
7.1k 粉丝
0 条评论
推荐阅读
Java资深工程师面试之chatGPT自问自答版
在这个岗位上,您需要展现您的Java编程技能、系统设计和架构能力、解决问题的能力以及领导和团队合作能力。下面是一些常见的Java资深工程师面试问题和答案,希望对您有所帮助。

雄狮虎豹阅读 743

封面图
Java8的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft32阅读 27.5k评论 1

一文彻底搞懂加密、数字签名和数字证书!
微信搜索🔍「编程指北」,关注这个写干货的程序员,回复「资源」,即可获取后台开发学习路线和书籍来源:个人CS学习网站:[链接]前言这本是 2020 年一个平平无奇的周末,小北在家里刷着 B 站,看着喜欢的 up 主视...

编程指北71阅读 33.7k评论 20

Java11的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft28阅读 19.3k评论 3

Java5的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft13阅读 21.8k

Java9的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft20阅读 15.4k

Java13的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft17阅读 11.2k

玉树临风,仙姿佚貌!

1.8k 声望
7.1k 粉丝
宣传栏