LeetCode--LinkedList--141.Linked List Cycle(Easy)
141. Linked List Cycle(Easy)2019.7.10
题目地址https://leetcode.com/problems/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?
solution
解法一
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode p = head; //游标指针
while (p != null && p.next != null) //注意循环的终止条件!!!!
{
p.val = Integer.MIN_VALUE; //将结点里面的值设为一个超小的值
if (p.next.val == Integer.MIN_VALUE) //判断是否有环
return true;
else
p = p.next;
}
return false;
}
}
解法二
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head, slow = head; //设置快慢指针,fast和slow
boolean flag = false;
while (slow != null && slow.next != null && fast != null && fast.next != null) //要格外注意循环的终止条件!!!!!
{
if (slow == fast && flag == true) //判断两指针是否相遇,且不是在初次出发处
return true;
else
{
flag = true;
slow = slow.next;
fast = fast.next.next;
}
}
return false;
}
}
reference
https://leetcode.com/problems/linked-list-cycle/solution/
总结
题意是给定一个链表,判断此链表里面是否有环,题目的附加要求是只用O(1)的空间解决此题。
- 解法一是直接遍历链表,将链表里面当前结点的val值设置为一个极小的数Integer.MIN_VALUE,然后判断当前结点的下一个结点值val是否为Integer.MIN_VALUE,若是,则返回true,否则再判断下一个结点,如果循环结束还没找到,则返回false。
- 解法二利用了快慢指针有环必相遇的思想。首先设置一个slow指针,每次走一步,然后设置一个fast指针,每次走两步,若链表有环,则若干步后两个指针必相遇。解法里面设置了一个flag变量来控制第一次slow==fast时不会返回true。此种解法要格外注意while循环的结束条件,稍不留意,就会出错。
Notes
1.链表问题要格外注意循环的终止条件;
LeetCode--LinkedList--141.Linked List Cycle(Easy)的更多相关文章
- 【leetcode】Remove Linked List Elements(easy)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 【一天一道LeetCode】#141. Linked List Cycle
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】141. Linked List Cycle (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- LeetCode:14. Longest Commen Prefix(Easy)
1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- LeetCode 141. Linked List Cycle (链表循环)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- LeetCode算法题-Linked List Cycle(Java实现)
这是悦乐书的第176次更新,第178篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第35题(顺位题号是141).给定一个链表,确定它是否有一个循环. 本次解题使用的开发工 ...
随机推荐
- ASE project demo:pdf
欢迎使用 pdf ~ 主页面如下,整个app风格一致,保持简约舒适的视觉体验~ 侧边栏打开,可选择打开新的pdf文件,返回主页面,打开本地生词本,登录等操作~ 可以点击侧边栏OpenFile打开新的p ...
- string 中的getline
1 getline 读入string库中的字符串 string a; getline(cin,a); 这样的读入要比任何一种读入字符串都有要快 2 char a[N]; cin.getline(a, ...
- 【JAVA】并发-基础IO
一.java.io包支持.java的IO流有输入.输出两种,每种输入.输出流又可分为字节流.字符流两大类,字节流以字节为单位处理IO操作,字符流以字符为单位处理IO操作 JDK 1.4以后有java. ...
- OpenAL试水
参考了https://wysaid.org/976.html. 这个博客给了一个EGE+OpenAL的demo和源代码.一开始没注意,博主也没有给EGE相关信息.会找不到EGE相关头文件,建议如果要二 ...
- 操作google_sheets
起源:最近了使用flask和bootstrap写了测试小工具,数据全部使用excel存储,部署到测试环境. 问题:每次每个人在使用excel数据时都需要重新编辑好的excel通过upload按钮传到服 ...
- [linux] linux的top命令参数详解
简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...
- python安装pil库,操作流程以及安装中出现的问题。
0.用管理员方式打开cmd窗口. 1.跳转到python对应目录 比我: ***或者直接在该路径下输入cmd直接跳转.**** 例如: 直接回车搞定!! 2.输入 pip install pillow ...
- Android xUtils3.0使用手册(一)- 基础功能使用
xUtils3 其功能不得不说,简化了很多的开发步骤,可以说是非常好的开发工具,但是苦于没有完整的使用手册,下面是使用中的一些总结,不断完善. xUtils 版本 3.3.36 jar包下载地址 ht ...
- PIL库的学习总结及生成GIF
一.PIL库的概述 PIL(Python Image Library)库是Python语言的第三方库,需要通过pip工具安装. 打开cmd,输入 pip install pillow PIL库支持图像 ...
- 我的Android进阶之旅------> Android为TextView组件中显示的文本加入背景色
通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article ...