【Linked List Cycle II】cpp
题目:
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?
代码:
使用hashmap版
/**
* 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) {
std::map<ListNode *, bool> ifNodeOccured;
ListNode *p = head;
while ( p )
{
if ( ifNodeOccured.find(p) != ifNodeOccured.end() ) return p;
ifNodeOccured.insert(std::pair<ListNode *, bool>(p,true));
p = p->next;
}
return NULL;
}
};
不用hashmap版(O(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 false;
ListNode *p1 = head;
ListNode *p2 = head;
while ( p2->next && p2->next->next )
{
p2 = p2->next->next;
p1 = p1->next;
if (p2==p1)
{
p1 = head;
while (p1 != p2)
{
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
}
return false;
}
};
Tips:
个人还是喜欢hashmap的解法,因为比较统一;但当数据量很大的时候,开辟hashmap占用的资源也是蛮多的。
第二种解法参考的如下两篇日志:
http://www.cnblogs.com/hiddenfox/p/3408931.html
http://blog.csdn.net/cs_guoxiaozhu/article/details/14209743
这里只讨论有环的case:
快慢指针技巧;慢==快,则快走的路程是慢走的路程的2倍;再让慢从头走,快从相遇点走,但每次改走一步,则再次相遇的地方就是起点。
这个无论是head到相遇点多长,都满足,算是一个特殊的技巧吧。还是很喜欢hashmap的做法。
====================================================
第二次过这道题,大体思路还记得。hashmap的方法一次写出来AC了,特殊技巧的写法参照了之前的代码。
/**
* 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 head;
ListNode* p1 = head;
ListNode* p2 = head;
while (p2)
{
p1 = p1->next;
p2 = p2->next;
if (p2)
{
p2 = p2->next;
}
else
{
return NULL;
}
if ( p1==p2 )
{
p1 = head;
while (p1!=p2)
{
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
}
return NULL;
}
};
这里有个细节,需要记住:当p1=p2的时候,重置p1=head;接下来先要判断p1!=p2然后再开始走,否则,当整个链表是个环的时候,就出bug了。
【Linked List Cycle II】cpp的更多相关文章
- leetcode 【 Linked List Cycle II 】 python 实现
公司和学校事情比较多,隔了好几天没刷题,今天继续刷起来. 题目: Given a linked list, return the node where the cycle begins. If the ...
- 【Pascal's Triangle II 】cpp
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 【LeetCode练习题】Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- 【LeetCode】142. Linked List Cycle II (2 solutions)
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- [算法][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 ...
- 15. 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 solve i ...
- 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 ...
随机推荐
- linux命令模式下如何切换首行和尾行
G是到最后一行,gg是到第一行
- 使用CSS设置Chrome打印背景色
以下内容适用于Chrome浏览器 打印背景色可以通过在打印预览页面勾选背景图形实现 如果需要在用户不勾选的情况下依然能够打印背景色,可以通过css实现,如,table隔行设置背景色: .data-ta ...
- SqlServer查询文件组被占用情况
在SqlServer中,删除一个文件组 alter database [xxxxx] remove filegroup FGMonthTurnIntroduceByMonth13 有时候会遇到如下报错 ...
- SQLSERVER 创建ODBC 报错的解决办法 SQLState:'01000'的解决方案
错误详情如下: SQLState:'01000' SQL Server 错误:14 [Microsoft][ODBC SQL Server Driver][DBNETLIB] ConnectionOp ...
- ADO.NET #3-1 (GridView + DataReader + SqlCommand)完全手写Code Behind
[C#] ADO.NET #3-1 (GridView + DataReader + SqlCommand)完全手写.后置程序代码 之前有分享过一个范例 [C#] ADO.NET #3 (GridVi ...
- 为Visual Studio 2012添加MSDN离线帮助
之前有网络的情况下,一直使用的都是在线的,最近又有笔记本上面有时使用时没有网络,所以就想使用下离线的MSDN包.可是找了半天,发现都是需要再次进行下载的.VS2012使用的帮助程序是HelpViewe ...
- JavaScript模块化开发的那些事
模块化开发在编程开发中是一个非常重要的概念,一个优秀的模块化项目的后期维护成本可以大大降低.本文主要介绍了JavaScript模块化开发的那些事,文中通过一个小故事比较直观地阐述了模块化开发的过程. ...
- 【洛谷4657】[CEOI2017] Chase(一个玄学的树形DP)
点此看题面 大致题意: 有一棵树,树上编号为\(i\)的节点上有\(F_i\)个铁球,逃亡者有\(V\)个磁铁,当他在某个节点放下磁铁时,与这个节点相邻的所有节点上的铁球都会被吸引到这个节点.然后一个 ...
- iptables 防火墙详解
一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它分为硬件的或者软件的防火墙两种.无论是在哪个网络中,防火墙工作的地方一定是在网络的边缘.而我们的任务就是需要去定义到底防 ...
- 函数定义和调用 -------JavaScript
本文摘要:http://www.liaoxuefeng.com/ 定义函数 在JavaScript中,定义函数的方式如下: function abs(x) { if (x >= 0) { ret ...