LL141LinkedListCycle
来源:互联网 发布:java 生成汉字五笔码 编辑:程序博客网 时间:2024/06/01 08:39
思路
- use two pointers, one is fast, one is slow.
- When the two meet, it has cycle
- Bug:
- Have to make sure the pointer is not null, and its next is not null before assigning the pointer, e.g.
fast = fast.null
hasCycleTwoPointers2()
optimize the while condition.
- Have to make sure the pointer is not null, and its next is not null before assigning the pointer, e.g.
Remember
- if
ListNode ln = new ListNode(3)
, thenln.next == null
. The next pointer is always exists. So we don’t need to checkln.next != null
if we assignfast = ln.next
;
while(fast != null && fast.next != null) { fast = fast.next.next; if (fast == slow) return true; slow = slow.next; }
- Remove element while iterating the set is not elegant. http://stackoverflow.com/questions/1110404/remove-elements-from-a-hashset-while-iterating
0 0
- LL141LinkedListCycle
- Android 沉浸式状态栏及悬浮效果
- 使用git快捷方便的保存代码
- (抄)程序员技术练级攻略-陈皓
- Inspection工具窗口
- (抄)写给新手程序员的一封信
- LL141LinkedListCycle
- 清除浮动引起的高度塌陷的7种方法
- JavaScript 对比
- Java垃圾回收机制(GC)详解----垃圾回收GC(Garbage Collection)是Java语言的核心技术之一,之前我们曾专门探讨过Java 7新增的垃圾回收器G1的新特性,但在JVM的内部
- HT299BullsAndCows
- 实验驱动开发与响应式编程 ---- File Watcher的技术实现----File Watcher,是由我开发的一个文件监控工具(见:File Watcher,不只是一个工具),关于它的具体需求以
- 为东北师范而备考----第二次备考,我又回到了长春。那么这次我准备得就比上次要充分很多了。其实考东北师范大学的软件工程专业,相比于清华,是要容易很多的。因为四门考试内容是政治、英语一、数学二以及C程序
- (转)应该知道的Linux技巧-陈皓
- IDA6.6远程调试Android应用程序的一点总结