[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, ...
- TCL列表
列表是Tcl的基本可用数据类型之一.它是用于表示项目的有序集合.它可以包括不同类型的在同一列表的项目.此外,一个列表可以包含另一个列表. 需要注意的一个重要的事情是,列表表示为完全串并处理在需要时,形 ...
- 在ubuntu 18.04下,无线网卡无驱动,连不上wifi,显示wifi没有适配器的解决方法
近来因为做东西要用到linux环境,所以自己的笔记本在win10的系统上又安装了ubuntu 18.04版本的双系统,但是安装好以后,没有无线网卡的驱动,显示wifi没有适配器等字样,很纠结,前后研究 ...
- FTP和TCP、UDP
应用:TFTP客户端 1. TFTP协议介绍 TFTP(Trivial File Transfer Protocol,简单文件传输协议) 是TCP/IP协议族中的一个用来在客户端与服务器之间进行简单文 ...
- leetcode205
public class Solution { public bool IsIsomorphic(string s, string t) { if (s.Length != t.Length) { r ...
- css常用属性初总结:font
平时在做项目时,UX常说的一句话就是“这里的字体不对吧,字体大小也不太对,你们前端有没有按规范来”,今天,我们就一起来看看这折磨人的font属性. 字体属性font-family 允许值 系列名 初始 ...
- Work-Stealing in .NET 4.0
[Work-Stealing in .NET 4.0] 1.线程按LIFO取Task,因为最后一个Task很可能还在Cache中,提高命中率. 2.Stealer从FIFO取Task,最先加入的Tas ...
- 39. Combination Sum (Back-Track)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- jQuery的选择器的总结
一.简单选择器 // $(function () { // $("#box").css("color","red") // }) // 这个 ...
- memcached的常用命令
memcached 常用命令及使用说明 1.启动Memcache 常用参数 -p <num> 设置TCP端口号(默认设置为: 11211) -U <num> UDP监听端口 ...