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 ...
 
随机推荐
- 0019SpringBoot使用异步任务(多线程)与定时任务
			
SpringBoot开启异步任务只需要两步配置: 1.在主类上加上注解@EnableAsync开启异步功能 2.在service层的方法上加上注解@Async指定调用方法时是异步的 SpringBoo ...
 - CentOS 6.5下快速搭建ftp服务器
			
来源:Linux社区 作者:Linux CentOS 6.5下快速搭建ftp服务器 1.用root 进入系统 2.使用命令 rpm -qa|grep vsftpd 查看系统是否安装了ftp,若安装了v ...
 - fsLayuiPlugin树+数据表格使用
			
fsLayuiPlugin 是一个基于layui的快速开发插件,支持数据表格增删改查操作,提供通用的组件,通过配置html实现数据请求,减少前端js重复开发的工作. GitHub下载 码云下载 测试环 ...
 - Centos 拒绝ssh远程暴力破解方法
			
佳木斯SEO摘要 有一天突然收到一封邮件,邮件内容告知我的ECS服务器作为肉鸡在攻击别的机器,期初一想,一定是我机器的账号密码被泄露,或者是被人暴力破解,于是乎,我就查询了一下我机器的账号登录记录. ...
 - HTML5 Web SQL 数据库
			
呼和浩特seo:Web SQL 数据库 API 并不是 HTML5 规范的一部分,但是它是一个独立的规范,引入了一组使用 SQL 操作客户端数据库的 APIs. 如果你是一个 Web 后端程序员,应该 ...
 - django 渲染模板与 vue 的 {{ }} 冲突解决方法
			
如果不可避免的在同一个页面里既有 django 渲染又有 vue 渲染的部分,可有 2 种方式解决 方法一: 采用 vue 的 delimiters 分隔符. new Vue({ delimiter ...
 - php文件上传下载组件
			
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
 - Tomcat 解决jvm中文乱码,控制台乱码
			
解决方法 打开tomcat/conf/目录 修改logging.properties 找到 java.util.logging.ConsoleHandler.encoding = utf-8 这行 更 ...
 - 数据结构实验之图论九:最小生成树 (SDUT 2144)
			
#include<bits/stdc++.h> using namespace std; typedef long long ll; struct node { int s, e; int ...
 - Python3 内置http.client,urllib.request及三方库requests发送请求对比
			
如有任何学习问题,可以添加作者微信:lockingfree 更多学习资料请加QQ群: 822601020获取 HTTP,GET请求,无参 GET http://httpbin.org/get Pyth ...