[LeetCode 题解]: Linked List Cycle II
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?
题意:
给定一个链表,找到环起始的位置。如果环不存在,返回NULL。
分析:
(1)首先要判断该链表是否有环。如果没有环,那么返回NULL。
(2)其次,当已知环存在后,寻找环起始的位置。
思路:
(1)链表是否有环,利用快慢指针很好解决。
(2)当环存在时,寻找环的起始位置:
++++使用额外的空间,可以用一个map存放链表的节点,遍历链表:
如果当前节点在map中没有出现,那么将该节点加入到map中,否则,返回当前节点(也就是环的起点)。
-----不使用额外空间,利用快慢指针的特性。
1st ruond:
SP(start point) MP(match point)
A: --------------------------------------------------------MP
B: - - - - - - - - - - - - - MP 2nd round:
B: MP- - - - - -- - - - - - - - -MP
C:---------------------------------------------------------MP
3rd round:
B1:- - - - - - - - - - - - - MP
B2: MP- - - - - - - - - - - - - - MP B1与B2必定会在途中相遇。 那么,他们首次相遇的点即为环的起始点。
设定两个指针A,B都从SP(Start point)出发: A每次向后移动两个节点,B每次向后移动一个节点。
假设A,B在MP(match point)相遇。 那么A所走的路径长度为B所走的路径长度的2倍:La = 2*Lb。
假设有另一个指针C还是从SP启动,每次向后移动两个节点, B从MP开始出发每次向后移动一个节点,那么可以想象C和B会在MP相遇。
如果C从SP出发,每次只移动一个节点,B依旧从MP出发且每次向后移动一个节点, 那么B和C依旧会在MP相遇。
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
// list length less than two
if(head==NULL || head->next==NULL) return NULL;
// if cycle exists in the list
ListNode *startA= head->next;
ListNode *endA= head->next->next;
while(endA!=NULL && endA != startA){
endA=endA->next;
if(endA){
endA=endA->next;
startA=startA->next;
}
}
// no cycle exists
if(endA==NULL) return NULL;
// find a cycle then search the start point
ListNode *match = startA;
ListNode *start = head;
while(start!=match){
start = start->next;
match = match->next;
}
return start;
}
};
[LeetCode 题解]: Linked List Cycle II的更多相关文章
- [Leetcode Week6]Linked List Cycle II
Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...
- Java for LeetCode 142 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 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
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【题解】【链表】【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 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- (链表 双指针) 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 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- ffmpeg+nginx+video实现rtsp流转hls流,通过H5查看监控视频
一.FFmpeg下载:http://ffmpeg.zeranoe.com/builds/ 下载并解压FFmpeg文件夹,配置环境变量:在“Path”变量原有变量值内容上加上d:\ffmpeg\bin, ...
- 除去a标签target="_blank"的方法
用Jquery:$(function(){$("div>a").attr("target","_blank");});先查找页面上的d ...
- Train-Alypay-Cloud:mPaaS 移动开发平台培训(第一次)
ylbtech-Train-Alypay-Cloud:mPaaS 移动开发平台培训(第一次) 1.返回顶部 1. 大家好! 欢迎大家参加蚂蚁金融云 即将在2018年1月17日到1月18日 在北京 环球 ...
- CentOS防火墙中端口的开启和关闭
1.开启端口(以80端口为例) 方法一: /sbin/iptables -I INPUT -p tcp --dport -j ACCEPT 写入修改 /etc/init.d/iptables save ...
- IDEA试用期结束以后继续试用(全部失效就更新),IDEA 2018 LICENSE SERVER
IDEA是一款收费的IDE,但是新用户可以免费试用一段时间,试用期结束可以购买,也可以通过填写License server address来继续使用. 打开IDEA以后,通过Help ----- Re ...
- [Windows报错]要求的函数不受支持、这可能是由于 CredSSP 加密 Oracle 修正
版本说明: 服务器版本:Windows Server 2008 R2 SP1(虚机) 客户端版本:Windows 10 家庭版 问题描述: 使用Windows远程桌面连接时弹出如下描述的错误,如图 ...
- springboot 2.0 自定义redis自动装配
首先创建maven项目 pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xm ...
- Windows和Linux双系统下完美卸载linux
装了Windows和linux双系统的朋友,在后期要删除linux是个比较头痛的问题,因为MBR已经被linux接管,本文的目的是如何在windows 和linux双系统下,简单,完美地卸载linux ...
- set 续4
---------siwuxie095 批处理简易计算器 @echo off ::设置窗口标题 title DOS 简易计算器 ::设置窗口大小 ::设置窗口颜色 co ...
- Linux查看用户及分组
cat /etc/group文件包含所有组cat /etc/shadow和cat /etc/passwd系统存在的所有用户名 usermod 或者修改 /etc/passwd文件即可修改当前用户所属组 ...