141. Linked List Cycle (amazon)
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
Solution: solve the problem with one and two steps
no cycle case: the faster pointer(two step) reaches the null first
cycle : slower == faster
Caution: check the null case
/**
* 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) {
if(head == null) return false;
if(head.next == null) return false;
//with extra space
//a -> b ->c -> d -> a;
ListNode one = head;
ListNode two = head;
while(two.next != null && two.next.next !=null){
one = one.next;
two = two.next.next;
if(one==two) return true;
}
return false;
}
}
141. Linked List Cycle (amazon)的更多相关文章
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 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 ...
- 141. Linked List Cycle - LeetCode
Question 141. Linked List Cycle Solution 题目大意:给一个链表,判断是否存在循环,最好不要使用额外空间 思路:定义一个假节点fakeNext,遍历这个链表,判断 ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- 【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] 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 ...
随机推荐
- stark组件之创建
stark组件之需求 仿照Django中的admin , 开发了自己的stark组件,实现类似数据库客户端的功能,对数据进行增删改查 . stark之创建 1.在项目中 创建stark应用,app01 ...
- linux学习3(vim)
一.文档编辑 1. vi和vim命令 Vim的打开文件的方式(4种,要求掌握的就前三种): 1. vim 文件路径 ...
- MYSQL生成两个日期之间的所有日期数据
set @i = -1; set @sql = repeat(" select 1 union all",-datediff('2021-01-01','2030-12-31')+ ...
- 性能测试工具LoadRunner11-LR之Virtual User Generator 移动app录制
准备条件: 1.安装插件LR_03105_Patch4.EXE,安装完成之后就会有Mobile App(HTTP/HTML),如下截图所示 2.安装热点wifi,160wifi(注:有可能有的热点软件 ...
- [转]Using the HTML5 and jQuery UI Datepicker Popup Calendar with ASP.NET MVC - Part 4
本文转自:http://www.asp.net/mvc/overview/older-versions/using-the-html5-and-jquery-ui-datepicker-popup-c ...
- [转]jquery插件创建 jquery.fn.extend与jquery.extend
本文转自:http://www.cnblogs.com/wyjgreat/archive/2011/07/19/2110754.html jQuery为开发插件提拱了两个方法,分别是: JavaScr ...
- Murano Weekly Meeting 2016.05.31
Meeting time: 2016.May.31 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1.A ...
- (转)Shell脚本之break,continue,和exit区别
Linux脚本中的break continue exit return break结束并退出循环 continue在循环中不执行continue下面的代码,转而进入下一轮循环 exit退出脚本,常带一 ...
- DetachedCriteria用法
http://uule.iteye.com/blog/947923转载 在常规的Web编程中,有大量的动态条件查询,即用户在网页上面自由选择某些条件,程序根据用户的选择条件,动态生成SQL语句,进行查 ...
- log4net 基础
log4net:日志输出工具. 新建工程Log4NetDemo App.config配置如下: <?xml version="1.0" encoding="utf- ...