LeetCode——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?
原题链接:https://oj.leetcode.com/problems/linked-list-cycle/
题目:给定一个链表。推断它是否有环。
继续:
你能不用额外的空间解决吗?
思路:使用两个指针fast,slow,fast每次向前走两步,slow每次向前走一步。假设二者相应的节点相等。即存在环。
public boolean hasCycle(ListNode head) {
ListNode fast = head,slow = head;
if(head == null || head.next == null)
return false;
while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
if(slow == fast)
return true;
}
return false;
}
// Definition for singly-linked list.
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
LeetCode——Linked List Cycle的更多相关文章
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [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 ...
- [算法][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 ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode: Linked List Cycle 解题报告
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- LeetCode Linked List Cycle 解答程序
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
随机推荐
- JavaScript拖拽
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta http-e ...
- iscroll横向滑动(当前页状态标记自动变化)
var myScroll; function loaded(){ myScroll = new iScroll('wrapper',{ snap:true, checkDOMChanges:true, ...
- Sql Server2000,2005,2008各版本主要区别
Emerson回来之后,在过程中遇到的一些问题,再次做一些整理,包括本篇的Sql Server各版本之间的区别和另一篇数据库函数. (博文内容来自网络) 数据类型 SQL Server 2008 数据 ...
- C# this.Invoke()的作用和用法(摘)
Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke(); 在多线程编程中,我们经常要在工作线程 ...
- 使用URLClassLoader类载入类实例
Tomcat当中在接受到要调用的Servlet请求后,需要载入相应的Servlet类,然后创建Servlet类实例,从而调用Servlet类实例的service()方法 下面实例的场景,request ...
- 写的一个Makefile
#========================================================================= # # MAKE FILE FOR ROCKY # ...
- Debug和Release之本质区别
转自Debug和Release之本质区别 Debug 和 Release 编译方式的本质区别 Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release 称为发 ...
- Android 进入另外一个窗体的两种方法
方法一: Intent intent = new Intent(); intent.setClassName(this, "com.wuyou.twoactivity.OtherActivi ...
- win7电脑自动关机怎么设置
WIN7系统自带了关机工具的,下面是步骤 1.“开始”-右键点击“计算机”选择“管理”,在左侧界面中选择“任务计划程序”. 2.在右侧界面中选择“创建基本任务”(向导式创建任务,推荐新手使用)或者“创 ...
- bzoj1560
首先这种题目肯定是要先排序,以x为第一关键字,y为第二关键字不难想到O(n2)的dp,下面显然要优化不难发现,由于两点的耗费是坐标差的平方的和,不带根号,因此,不难发现一个很有用的性质,如果从A点能到 ...