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 extra space?
题目标签:Linked List
题目给了我们一个 Linked List,让我们判断它是否循环。
利用快,慢指针,快指针一次走2步,慢指针一次走1步,如果循环,快慢指针一定会相遇。
Java Solution:
Runtime beats 98.15%
完成日期:06/09/2017
关键词:singly-linked list;cycle
关键点:fast, slow pointers
/**
* 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; ListNode slow = head;
ListNode fast = head; do
{
if(fast.next == null || fast.next.next == null) // if fast reaches to end, it doens't have a cycle
return false; fast = fast.next.next; // fast moves 2 steps
slow = slow.next; // slow moves 1 step } while(fast != slow); return true; // if fast catches slow, meaning it has a cycle
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 141. Linked List Cycle (链表循环)的更多相关文章
- [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 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- [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 判断链表是否有环 C++/Java
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判断链表是否有环
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- (链表 双指针) 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环形链表 (C++)
题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
随机推荐
- Microsoft SQL Server学习(三)
1.表:表示一个实体(客观存在的事物或抽象时间),可实现对实体的数据描述和数据操作. 2.表结构:二位平面(行.列) 3.数据类型: 类型名称 类型 整形 bit(只存储0.1) samllint i ...
- STA之Concepts (1)
Static Timing Analysis is one of the many techniques available to verify the timing of a digital des ...
- canvas一周一练 -- canvas绘制立体文字(2)
运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...
- Codeforces_768_B_(二分)
B. Code For 1 time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 10.5 集合ArrayList 和 io流
1.ArrayListToFile package day10_io_fileWrite_Read.arraylist_tofile; import java.io.BufferedWriter; i ...
- Java之希尔排序
希尔排序 前面已经知道了插入排序,明白插入排序的原理,不断比较来交换相邻的元素,这样的话效率不高,为此希尔排序,在插入排序上做出了改进,通过间隔增量来比较并交换元素,这样可以减少比较交换的次数. pa ...
- Android studio升级后原有项目无法正常编译运行问题
Android studio工具升级后Gradle版本问题 背景 升级AndroidStudio到最新版本后,原来可正常编译输出AndroidTest的项目无法正常编译通过. 原因 升级后的Andro ...
- 15Ajax、JSON
15Ajax.JSON-2018/07/27 1. ThreadLocal 总结:调用该类的get方法,永远返回当前线程放入的数据.线程局部变量. 保证线程安全 (第二阶段day14后半部分视频以及1 ...
- Linux学习笔记记录(七八)
- CentOS7 安装 PHP7.2
点击查看原文 安装源 安装 EPEL 软件包: $ sudo yum install epel-release 安装 remi 源: $ sudo yum install http://rpms.re ...