题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL。

思路:

  (1)依然用两个指针的追赶来判断是否有环。在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路(即1跑过,2也跑过),就是那段(环开始处,相遇处),那么指针2开始到环开始处的距离与head到指针相遇处是等长的喔~,那么再跑一次每次一步的就必定会相遇啦。画个图图好方便看~

  (2)其实还有另一个直观的思路,就是指针1和2相遇后,p指向他们的next,在他们相遇处的next给置空,再跑一遍那个“找两个链表后半段重叠的开始处”那道题就行了。

(1)代码

 /**
* 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) {
if(!head) return ;
ListNode *one=head, *two=head->next;
while(two&&two->next&&one!=two)
{
one=one->next;
two=two->next->next;
}
if(!two||!two->next) return ; //无环
two=two->next;//此时他们已经相遇了,two后移一步,使two与head同时到one等长。
while(head!=two)//必定会相遇
{
head=head->next;
two=two->next;
}
return head;
}
};

AC代码

(2)代码

 /**
* 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) {
if(!head) return ;
ListNode *one=head, *two=head->next;
while(two&&two->next&&one!=two)
{
one=one->next;
two=two->next->next;
}
if(!two||!two->next) return ; //无环
one=one->next; //此时two还在断口处
two->next=; ListNode * p1=one, *p2=head;
while(p1 && p2 && p1!=p2 )//两链表找重叠处~即使p1p2到开始重叠处不等长也能解决
{
p1=p1->next;
p2=p2->next; if(!p1) p1=head;
if(!p2) p2=one;
}
two->next=one;
return p1;
}
};

AC代码

LeetCode Linked List Cycle II 单链表环2 (找循环起点)的更多相关文章

  1. [LeetCode] Linked List Cycle II 单链表中的环之二

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

  2. [算法][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 ...

  3. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

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

  4. [Leetcode] Linked list cycle ii 判断链表是否有环

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

  5. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  6. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  7. [LeetCode] Linked List Cycle II 链表环起始位置

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

  8. leetcode 142. Linked List Cycle II 环形链表 II

    一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点  head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...

  9. [LeetCode] Linked List Cycle II, Solution

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

随机推荐

  1. HDU4718 The LCIS on the Tree(LCT)

    又是一枚LCT,写一发加深一下对LCT的理解.本题的坑爹之处就在于,它实在是太坑爹了.询问的是树路径上的最长连续上升的子串,考验的是怎么样去维护.一开始的想法是维护三个变量 ls,rs,mxl,分别表 ...

  2. UITableView局部刷新

    只刷新cell不刷新section,这问题还难住了一阵子 需要用到: - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnima ...

  3. Ehcache使用

    http://www.360doc.com/content/14/0423/17/16946725_371472946.shtml http://www.myexception.cn/web-appl ...

  4. ExtJs布局之Card

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  5. 高性能js之js加载执行

    转载自:http://www.blogjava.net/BearRui/archive/2010/04/08/web_performance_js_where.html 外部JS的阻塞下载 所有浏览器 ...

  6. 实现Web上的用户登录功能

    关于如何实现web上的自动登录功能 文章来源http://coolshell.cn/articles/5353.html Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能 ...

  7. AcmeAir安装AI探针--企业版

    通过脚本安装AI探针请点击通过脚本自动安装探针 一.安装企业版AI探针准备工作: 1. 准备好可用的docker版AcmeAir应用 2. 准备好可用的企业版AIServer 3. 下载好合适版本的J ...

  8. Data Flow ->> DQS Cleansing

    Data Quality Services(DQS)是SQL Server 2012引入的一大特性.这个服务的任务是为了实现客户端数据标准化和清理错误数据的.比如客户端数据容易因为用户输出诸如像城市名 ...

  9. Hibernate的配置文件解析

    配置mybatis.xml或hibernate.cfg.xml报错: <property name="connection.url">jdbc:mysql://loca ...

  10. Delphi文件夹的操作

    /// <remarks> /// 重命名文件夹 引用ShellAPI /// </remarks> function ReNameDirectort(SourceDirect ...