141. Linked List Cycle

Easy

Given a linked list, determine if it has a cycle in it.

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Example 1:

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

Example 2:

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

Example 3:

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

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

package leetcode.easy;

/**
* Definition for singly-linked list. class ListNode { int val; ListNode next;
* ListNode(int x) { val = x; next = null; } }
*/
public class LinkedListCycle {
public boolean hasCycle1(ListNode head) {
java.util.Set<ListNode> nodesSeen = new java.util.HashSet<>();
while (head != null) {
if (nodesSeen.contains(head)) {
return true;
} else {
nodesSeen.add(head);
}
head = head.next;
}
return false;
} public boolean hasCycle2(ListNode head) {
if (head == null || head.next == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
} @org.junit.Test
public void test1() {
ListNode ln1 = new ListNode(3);
ListNode ln2 = new ListNode(2);
ListNode ln3 = new ListNode(0);
ListNode ln4 = new ListNode(-4);
ln1.next = ln2;
ln2.next = ln3;
ln3.next = ln4;
ln4.next = ln2;
System.out.println(hasCycle1(ln1));
System.out.println(hasCycle2(ln1));
} @org.junit.Test
public void test2() {
ListNode ln1 = new ListNode(1);
ListNode ln2 = new ListNode(2);
ln1.next = ln2;
ln2.next = ln1;
System.out.println(hasCycle1(ln1));
System.out.println(hasCycle2(ln1));
} @org.junit.Test
public void test3() {
ListNode ln1 = new ListNode(1);
ln1.next = null;
System.out.println(hasCycle1(ln1));
System.out.println(hasCycle2(ln1));
}
}

LeetCode_141. Linked List Cycle的更多相关文章

  1. [LeetCode] Linked List Cycle II 单链表中的环之二

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

  2. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  3. [LintCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...

  4. [算法][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 so ...

  5. LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...

  6. 15. 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 i ...

  7. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  8. Java for LeetCode 142 Linked List Cycle II

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

  9. 【题解】【链表】【Leetcode】Linked List Cycle II

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

随机推荐

  1. 0017SpringBoot注册Servlet三大组件(Servlet、Filter、Listener)

    由于SpringBoot默认是以jar包的形式启动嵌入式servlet容器来启动SpringBoot的web应用,所以没有web.xml文件,那么如何配置Servlet.Filter.Listener ...

  2. 使用 ajax 多次请求,并将结果集合并(ajax 非异步)

    直接上代码吧... 里面有注释 <!DOCTYPE html> <html> <head> <meta charset="utf-8" / ...

  3. Django Forms的错误提示

    1.error_messages={} 首先,在构建form表单时,可以用"error_messages={}"自定义错误信息,例如: # form.py 1 from djang ...

  4. Centos 改ssh 端口

    1.修改配置文件:/etc/ssh/sshd_config 2.修改 #Port 22 把前面的#注释删掉,然后添加一个Port XXXX 定义一个自己的.例如我喜欢 Port 22333 3.修改后 ...

  5. php MySQL 数据类型

    MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准S ...

  6. neo4j查询语句

    一:查询 比较操作: = <> < > <= >= 布尔操作: AND OR NOT XOR 1.把节点的前两个字为"提示"的节点去除" ...

  7. Oracle 修改SID --不建议修改

    1.登录数据库查看SID select instance_name,status from v$instance; 2.关闭数据库 shutdown immdiate; 3.修改/etc/oratab ...

  8. [Zhx] 无题

    https://www.luogu.org/problemnew/show/T15368 区间修改,区间查询k(<= 10)大值 应该也可以用分块写 #include <cstdio> ...

  9. linux 搭建elk6.8.0集群并破解安装x-pack

    一.环境信息以及安装前准备 1.组件介绍 *Filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停 ...

  10. Windows10+Jupyter notebook+添加核

    链接:https://blog.csdn.net/ZWX2445205419/article/details/80113472 1. 安装Anaconda   2. 创建虚拟环境   > con ...