LeetCode Linked List Cycle II 单链表环2 (找循环起点)
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回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 (找循环起点)的更多相关文章
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [算法][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 ...
- [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 ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- 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 ...
- 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 ...
- [LeetCode] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- leetcode 142. Linked List Cycle II 环形链表 II
一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点 head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
随机推荐
- Java开发WebService的几种方法--转载
webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2 Axis是apache下一个开源的webservice开发组件 ...
- CAP定理与RDBMS的ACID
一.分布式领域CAP理论 CAP定理指在设计分布式系统时,一致性(Consistent).可用性(Availability).可靠性(分区容忍性Partition Tolerance)三个属性不可能同 ...
- POJ 3279
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3062 Accepted: 1178 Descript ...
- 程序员必须知道的git托管平台
http://www.open-open.com/lib/view/open1420704561390.html
- java基础知识回顾之接口
/* abstract class AbsDemo { abstract void show1(); abstract void show2(); } 当一个抽象类中的方法都是抽象的时候,这时可以将该 ...
- mvc5 知识点01
1.ViewBag 动态数据类型,也就是说可以随便指定属性,前后台传值很是有用 2.Layout 属性,定义模版,模版中一般用@RenderBody() 做占位符,用于放置子页面内容 3.@model ...
- ExtJs布局之Column
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- nodejs的require模块及路径
在nodejs中,模块大概可以分为核心模块和文件模块. 核心模块是被编译成二进制代码,引用的时候只需require表示符即可,如(require('net')). 文件模块,则是指js文件.json文 ...
- asp.net中Literal与label的区别
Literal 控件表示用于向页面添加内容的几个选项之一.对于静态内容,无需使用容器,可以将标记作为 HTML 直接添加到页面中.但是,如果要动态添加内容,则必须将内容添加到容器中.典型的容器有 La ...
- php ++a和a++
<?php$a=$b=5;$a+$b=$a++-++$b;echo $b;?> 输出-1