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 ...
随机推荐
- P1363 幻象迷宫[搜索]
题目描述 (喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫.) WD:呜呜,肿么办啊-- LHX:momo...我们一定能走出去的! WD:嗯,+U+U ...
- dos中查找端口的PID,并在任务管理器中处理端口
本文来源https://www.cnblogs.com/lsyf/p/8979012.html 1.查看所有端口进程 首先点击开始菜单选择运行,接着在运行对话框中输入“cmd”,回车打开命令提示符窗口 ...
- LOJ 103子串查找——用hash代替kmp算法
题意 给出两个字符串 $s_1,s_2$,求 $s_2$ 在 $s_1$ 中出现的次数. 分析 预处理出两个字符串的哈希值,再逐位比较. 时间复杂度为 $O(n+m)$,和 $kmp$ 算法一样. 可 ...
- 006——转载-MATLAB数字与字符之间的转换
(一)参考文献:https://jingyan.baidu.com/article/5bbb5a1bd8dcb113eba1799d.html (二)数字转换成字符串 第一步在我们的电脑上打开matl ...
- spring boot, 容器启动后执行某操作
常有在spring容器启动后执行某些操作的需求,现做了一个demo的实现,做一下记录,也希望可以给需要的同学提供参考. 1.spring启动后,以新线程执行后续需要的操作,所以执行类实现Runnabl ...
- LOJ P10117 简单题 题解
每日一题 day15 打卡 Analysis 树状数组 用树状数组来维护每个字符变化的次数,如果是偶数就是0,奇数就是1 #include<iostream> #include<cs ...
- leetcode解题报告(21):Majority Element
描述 Given an array of size n, find the majority element. The majority element is the element that app ...
- selenium+chromeDriver配合使用(运行js脚本)
在python中调用selenium,访问百度,并运行js脚本爬取内容 python入口程序 from selenium import webdriver import time with open( ...
- macOs下安装Android SDK
首先下载 Android SDK for mac 配置安装 1.进入存放 android-sdk-macosx 的目录,然后输入./android sdk就可以调出SDK Manager的图形界面 A ...
- BIOS 中断大全
BIOS中断: 1.显示服务(Video Service--INT 10H) 00H -设置显示器模式0CH -写图形象素 01H -设置光标形状0DH -读图形象素 02H -设置光标位置0EH ...