linked-list-cycle
来源:互联网 发布:手机虚拟网络能否上网 编辑:程序博客网 时间:2024/05/22 04:55
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?
public class LinkedListCycle
{
//节点数据结构
static class ListNode
{
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public static ListNode detectCycle(ListNode head)
{
if(null == head)
{
return null;
}
//快慢指针
ListNode _slow = head;
ListNode _fast = head;
while(null != _fast.next && null != _fast.next.next)
{
_slow = _slow.next;
_fast = _fast.next.next;
//快慢指针相遇,说明有环
if(_slow == _fast)
{
ListNode _node1 = head;
ListNode _node2 = _fast;
while(_node1 != _node2)
{
_node1 = _node1.next;
_node2 = _node2.next;
}
return _node1;
}
}
return null;
}
}
- leetcode Linked List Cycle & Linked List Cycle ||
- Linked List Cycle II Linked List Cycle
- 【LeetCode】Linked List Cycle
- Leetcode: Linked List Cycle
- Leetcode Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle
- Linked List Cycle II
- Leetcode: Linked List Cycle
- LeetCode:Linked List Cycle
- Linked List Cycle II
- Linked List Cycle
- Linked List Cycle II
- [LeetCode]Linked List Cycle
- Linked List Cycle
- LeetCode | Linked List Cycle
- glusterfs分布式文件系统用作文件同步安装配置
- linux文件操作
- 3D坐标系:3D笛卡尔坐标系和3D柱面坐标系的区别
- To be or not to be that is a question
- 博客图片保存区
- linked-list-cycle
- 判断链表是否有环及环的入口
- caffe实现逐层可视化
- 字符串计数
- 搭建windows+php开发环境(imp)iis现在很强大哦
- MYSQL主从实战及读写分离
- poj3437
- 《JavaScript语言精粹》笔记
- 倍增法求最近公共祖先(LCA)