LeetCode_141. Linked List Cycle
141. Linked List Cycle
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的更多相关文章
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [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 ...
- [LintCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...
- [算法][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 ...
- 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 ...
- 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 ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- 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 ...
- 【题解】【链表】【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- [MVC] 自定义ActionSelector,根据参数选择Action[转载]
很多时候我们会根据UI传入的参数,呈现不同的View.也就是对于同一个Action如何根据请求数据返回不同的View.通常情况下我们会按照如下方法来写,例如: [AcceptVerbs(HttpVer ...
- Robot Framework--接口测试环境搭建
1.安装requests库 (robotframework-requests依赖这个request http client) 执行pip install requests 2. 安装requestLi ...
- CentOS环境部署(Nginx+Mariadb+Java+Tomcat)
1.安装nginx 安装 yum install nginx 启动 yum install nginx 开机自启 sudo systemctl enable nginx 2.安装mariadb 安装 ...
- Elasticsearch 调优之 写入速度优化到极限
基于版本: 2.x – 5.x 在 es 的默认设置,是综合考虑数据可靠性,搜索实时性,写入速度等因素的,当你离开默认设置,追求极致的写入速度时,很多是以牺牲可靠性和搜索实时性为代价的.有时候,业 ...
- PostgreSQL 配置参数
一.配置参数所在文件.类型与查看方式 1.配置参数所在文件 postgresql.conf 2.配置参数类型 1)internal只读参数 这些参数不配置在postgresql.conf中,他们由po ...
- https web service in Tibco & PC
Error: 1.Certificate signature validation failed , Signature does not matchuse wrong public certific ...
- Spring-RabbitMQ实现商品的同步(后台系统)
1.配置rabbitMQ 需要把以上配置文件加载到spring容器,在appliacationContext.xml中添加如下内容: 注意:无需配置监听,因为服务器端(生产者只需要将消息发送到交换机即 ...
- 一个简单的python实现百度登录操作
conftest作为全局调用,用于登录之后的其他元素使用,且保持登录状态的操作 在开始前需要搭建好测试环境,环境包含python+webdriver+浏览器驱动(此处为firefox) 1.conft ...
- 【java8新特性】日期和时间
Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本. Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ,它支持函数式编程,新的 JavaScript ...
- ICEM-轴(周期复制网格)
原视频下载地址:https://yunpan.cn/cqMnfpqQQdZZI 访问密码 802b