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 ...
随机推荐
- Avito Cool Challenge 2018:D. Maximum Distance
D. Maximum Distance 题目链接:https://codeforces.com/contest/1081/problem/D 题意: 给出一个连通图以及一些特殊点,现在定义cost(u ...
- Jenkins自动化CI CD流水线之3--参数化构建
一. 背景 如果只是简单的构建,jenkins自己默认的插件可以做,但是如果我们想要在构建过程中有更多功能,比如说:选择性构建.传参.项目指定变量等等其他功能,基础的参数化构建可以实现一些简单功能,但 ...
- Jenkins自动化CI CD流水线之2--用户权限管理
一. 背景 针对开发.运维.测试针对不同角色进行不同权限划分, 基于插件: Role-based Authorization Strategy来实现. 一. 安装 安装该插件: 系统管理->管理 ...
- vue(2)创建项目
1.创建项目 cmd到自己指定目录下,执行 vue init webpack-simple hello-vue 2.安装项目依赖 cd hello-vue cnpm install 3.运行该项目,测 ...
- Robot Framework常用库简介
标准库 Robot Framework可以直接导入使用的库,包括: • Builtin:包含经常需要的关键字.自动导入无需import,因此总是可用的 • Dialogs:提供了暂停测试执行和从用户的 ...
- 3dsmax 卸载/安装失败/出错 2019/2018/2017/2016/2015/2013/2012
AUTO Uninstaller 更新下载地址 1.选择3dsmax 2.选择版本 3.点击“开始卸载”
- 【LDAP】LDAP 中 CN, OU, DC 的含义
1. LDAP的存储规则 区分名(DN,Distinguished Name) 和自然界中的树不同,文件系统/LDAP/电话号码簿目录的每一片枝叶都至少有一个独一无二的属性,这一属性可以帮助我们来区别 ...
- HDU 5384——Danganronpa——————【AC自动机】
Danganronpa Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- Log4.Net日志记录解析
http://www.cnblogs.com/neekerss/archive/2011/01/04/1925171.html
- mysql-查询的案例
查询每个专业的男生人数和女生人数分别是多少 #方式一: select count(*) 个数,sex,majorid from student group by sex,majorid; #方式二: ...