Linked List Cycle II
2015-08-10 21:52
351 查看
Given a linked list, return the node where the cycle begins. If there is no cycle,
return null.
Follow up:
Can you solve it without using extra space?
当快指针追上慢指针时,一个指针从head开始,一个指针从追上的位置开始,
每次走一步,相遇是就是环的开始节点。
return null.
Follow up:
Can you solve it without using extra space?
当快指针追上慢指针时,一个指针从head开始,一个指针从追上的位置开始,
每次走一步,相遇是就是环的开始节点。
public ListNode detectCycle(ListNode head) { if (head == null) return null; ListNode fast, slow; fast = head.next; slow = head; while (fast != slow) { if(fast==null || fast.next==null) return null; fast = fast.next.next; slow = slow.next; } while(head!=fast.next){ head=head.next; fast=fast.next; } return head; }
相关文章推荐
- 数据结构06:排序
- 一起做RGB-D SLAM (5)
- ItemsControl 使用Grid布局
- Palindrome
- PHP数据类型
- Ubuntu Kylin 14.04 64位系统安装Android Studio
- tableViewCell - 常见设置
- Java实现斐波那契数列Fibonacci
- Android(java)学习笔记148:Eclipse中代码提示去掉@override,不然就报错!
- 蘑菇街一面
- 自动化运维工具Ansible之Python API
- java的守护线程与非守护线程
- java中的取整与四舍五入
- HDOJ 题目4714 Tree2cycle(树形DP)
- Linked List Cycle
- 自己动手实现数据结构——排序算法1(冒泡、插入、归并、简单选择)(C++实现)
- HDU-1667 The Rotation Game(IDA*)
- The OAuth 2.0 Authorization Framework-摘自https://tools.ietf.org/html/rfc6749
- POJ 2750 Potted Flower(线段树 + DP)
- 串讲Apache OFBiz技术架构