Leetcode_141_Linked List Cycle
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42833739
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
思路:
(1)题意为判断一个链表是否带环。这道题在校招笔试和面试中出现的次数还是比较多的。
(2)要判断是否带环,只需设置两个指针,从链表的起始点开始,分别往后遍历链表中的节点,如果在遍历的过程中两指针相遇,说明链表带有环。但是为了让它们在链表带环的情况下能够相遇,需要对两指针设置不同的步调,一个指针每次前进一步,另一个指针一次前进两步,这样如果前一个指针遍历到最后一个节点为空,则说明链表不带环;否则在遍历的过程中如果第一个指针和第二个指针相遇,则说明链表带环。
(3)希望本文对你有所帮助。
算法代码实现如下:
/**
* @author liqq
*/
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null)
return false;
ListNode curr = head;
ListNode sec = head;
while (sec.next != null && sec.next.next != null) {
curr = curr.next;
sec = sec.next.next;
if (curr == sec) {
return true;
}
}
return false;
}
Leetcode_141_Linked List Cycle的更多相关文章
- 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常. 意思是出现了死循环,也就是Model之间有循环包含关系: ...
- JS案例之2——cycle元素轮播
元素轮播效果是页面中经常会使用的一种效果.这个例子实现了通过元素的隐藏和显示来表现轮播效果.效果比较简单. 效果图如下: 源代码如下: <!DOCTYPE html> <html&g ...
- [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 ...
- [LintCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...
- UVA11090 Going in Cycle!! [spfa负环]
https://vjudge.net/problem/UVA-11090 平均权值最小的回路 为后面的做个铺垫 二分最小值,每条边权减去他,有负环说明有的回路平均权值小于他 spfa求负环的时候可以先 ...
- [算法][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 ...
- Life Cycle of Thread – Understanding Thread States in Java
Life Cycle of Thread – Understanding Thread States in Java 深入理解java线程生命周期. Understanding Life Cycle ...
- 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 ...
随机推荐
- Java对象的内存布局以及对象所需内存大小计算详解
1. 内存布局 在HotSpot虚拟机中,对象的内存布局可以分为三部分:对象头(Header). 实例数据(Instance Data)和对齐填充(Padding). 1) 对象头(Header): ...
- Android自定义View(RollWeekView-炫酷的星期日期选择控件)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/53420889 本文出自:[openXu的博客] 目录: 1分析 2定义控件布局 3定义Cus ...
- [BBS]搭建开源论坛之Jforum搭配开源CKEDITOR
本文作者:sushengmiyan 本文地址:http://blog.csdn.net/sushengmiyan/article/details/47946065 使用默认的编辑器的时候,格式都无法保 ...
- Android-Tab
SmartTabLayout 我的地址:https://github.com/kongqw/Android-Tab 开源地址:https://github.com/ogaclejapan/SmartT ...
- Programming In Scala笔记-第九章、控制抽象
本章主要讲解在Scala中如何使用函数值来自定义新的控制结构,并且介绍Curring和By-name参数的概念. 一.减少重复代码 1.重复代码的场景描述 前面定义的函数,将实现某功能的代码封装到一起 ...
- Effective Python 中文版
如题,博主正在翻译一本Python相关的书. 图为Python作者. [美]Brett Slatkin的名作. Effective Python: 59 Specific Ways to Write ...
- JBOSS EAP 6 系列六 公共模块的jar配置到jboss的modules详细配置
公司项目中遇到并要解决的问题 1:原则上除了自己写的代码之外,公共的jar不应该都在打包的时候打包到ear里面,这样的话包太大,也不符合的分层的逻辑,在jboss容器内部,每个ear的包重复jar都会 ...
- Redis工作系列之一 与 Memcached对比理解
近期公司项目在使用Redis,这几年Redis很火,Redis也常常被当作Memcached的挑战者被提到桌面上来.关于Redis与Memcached的比较更是比比皆是.然而,Redis真的 ...
- CoreText精彩文字轮廓绘制动画的一点改进
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 原文在: http://oleb.net/blog/2010/ ...
- SQLite Select 语句(http://www.w3cschool.cc/sqlite/sqlite-select.html)
SQLite Select 语句 SQLite 的 SELECT 语句用于从 SQLite 数据库表中获取数据,以结果表的形式返回数据.这些结果表也被称为结果集. 语法 SQLite 的 SELECT ...