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?

思路:

做过,就当复习了。

先用快慢指针判断相交,关键是环开始点的获取。

用上图说明一下,设非环的部分长度为a(包括环的入口点), 环的长度为b(包括环的入口点). 快慢指针相交的位置为绿色的点,距离环的开始位置为c(以环的入口点距离为0算)。

因为快指针的速度是慢指针的两倍。 故:  a + f*b + c = 2(a + s*b + c)

得出:         a + c = k * b

即此时一个指针从相交点开始走,距离入口为c, 再走一个a的距离 就会到达入口点,也就是说从相交点开始走的指针会和从头结点开始走的指针在入口点相交。 返回这个交点就可以了。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; // Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode * fast = head;
ListNode * slow = head;
ListNode * p = NULL;
//用快慢指针判断是否有环 快指针一次走两步 慢指针一次一步 有环会相遇
while(fast != NULL)
{
fast = (fast->next == NULL) ? NULL : fast->next->next;
slow = slow->next;
if(slow != NULL && slow == fast)
{
p = slow; //记录相遇的位置
break;
}
}
if(p == NULL) //如果没有相遇 没有环
{
return NULL;
}
else
{
ListNode * cross = head;
while(cross != p) //新的结点从头开始走 另一节点从之前相遇的位置开始走 这两个结点相遇的位置就是环的入口
{
cross = cross->next;
p = p->next;
}
return cross;
}
} };

【leetcode】Linked List Cycle II (middle)的更多相关文章

  1. 【leetcode】Path Sum I & II(middle)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  2. 【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  3. 【题解】【链表】【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  4. 【LeetCode】Linked List Cycle II(环形链表 II)

    这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...

  5. 【leetcode】Swap Nodes in Pairs (middle)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  6. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  7. 【leetcode】Binary Search Tree Iterator(middle)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. 【leetcode】Validate Binary Search Tree(middle)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. 【leetcode】Minimum Size Subarray Sum(middle)

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

随机推荐

  1. git之tag

    参考:git命令之git tag 给当前分支打标签 1.git tag //查看已有标签 2.创建本地标签 git tag tag_name  //创建标签  git tag -a v0.1.2 -m ...

  2. gc是什么,什么时候需要gc

    Java是由C++发展来的. 它摈弃了C++中一些繁琐容易出错的东西.其中有一条就是这个GC. 写C/C++程序,程序员定义了一个变量,就是在内存中开辟了一段相应的空间来存值.内存再大也是有限的,所以 ...

  3. git上传github上

    1.git init --初始化git   (选择文件夹) 2.git add README  --添加项目(项目的文件夹) 3.git commit -m "SSM(360)" ...

  4. iOS开发——UI进阶篇(六)键盘处理

    一.键盘通知我们经常需要在键盘弹出或者隐藏的时候做一些特定的操作,因此需要监听键盘的状态 键盘状态改变的时候,系统会发出一些特定的通知UIKeyboardWillShowNotification // ...

  5. leetcode 215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  6. UIView动画效果

    做出UI界面,实现程序功能,是重中之重,但是通过动画提升使用体验,一般人应该不会拒绝吧. 那么问题又来了,怎么做? 一: 稳扎稳打: 一步一步来吧,毕竟,心急吃不了热豆腐. 1.开启一个动画 2,设置 ...

  7. iOS网络学习之“远离NSURLConnection 走进NSURLSession”

    目前,在iOS的开发中,NURLConnection已经成为了过去式,现在的NSURLConnection已经deprected(iOS7之后),取而代之的是NSURLSession.而且AFNetw ...

  8. 最简单的Android教程之自定义控件

    新建title.xml,完成布局 新建一个TitleLayout继承 LinearLayout. activity_main.xml中引用 Run your applicaiton , and try ...

  9. 5-python学习——条件语句

    5-python学习——条件语句 5-python学习——条件语句 条件语句if else形式 if else条件语句说明 测试一下 编程语言一般都由这么几个部分组成 变量 条件分支语句 循环语句 函 ...

  10. centos python2.6升级到2.7 还有单独的python3.5环境

    查看python版本 #python -V Python 1.下载Python-2.7.3 #wget http://python.org/ftp/python/2.7.3/Python-2.7.3. ...