[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 ...
随机推荐
- java DecimalFormat类
今天去面试了,需要上机做题.题目的内容是计算一个货物订单的税费和总价格(包括税费),结果需要精确到两个小数,同时按照如下规则进行处理: 3.01 ——>3.05, 2.48——> ...
- ubuntu 下出现E: Sub-process /usr/bin/dpkg returned an error code
在用apt-get安装软件时出现了类似于 install-info: No dir file specified; try –help for more information.dpkg:处理 get ...
- CVE-2018-8420 漏洞复现
影响的 Windows 版本: Microsoft Windows 10 Version 1607 for 32-bit SystemsMicrosoft Windows 10 Version 160 ...
- 20165233 实验一 Java开发环境的熟悉
20165233 实验一 Java开发环境的熟悉 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程: 2.完成实验.撰写实验 ...
- 关于C语言中%p和%X的思考
说白了,(%A)仅仅代表以何种格式显示所要显示的数据,具体何种格式如下: %d 有符号10进制整数 %i 有符号10进制整数 %o 无符号8进制整数 %u 无符号10进制整数 %x 无符号的16进制数 ...
- Firefox 被Snap.do 劫持
Firefox IE 被劫持 windows的控制面板,卸载程序, 找任何跟「Snap.do」有关的软件,找到後先移除「Snap.Do Engine」再移除「Snap.Do」. 注册表里查找 Snap ...
- 使用libcurl显示下载进度
使用libcurl显示下载进度 http://blog.csdn.net/zhouzhenhe2008/article/details/53876622
- NotePad++替换行前、行后空格,替换空行
用 Notepad++ 打开,把每一个将要放在表中单元格的内容放一行(注: ^ 代表行首 $ 代表行尾) 去除行尾空格和空白行:按CTRL+H 选择正则表达式– 查找目标:\s+$ 替换为空 去除行首 ...
- “MSDTC 事务的导入失败: Result Code = 0x8004d00e。
今天搞数据库移植,也把分布式程序移植了一下,一直报上面的错误,差点要重装了 %>_<%,网上看了几篇文章偶然间用DPCPING工具测试了下连接,发现不行.想到host文件的配置,发现其中 ...
- SELinux导致的docker启动失败
安装docker yum install -y docker 启动docker systemctl start docker 报错 Job for docker.service failed beca ...