【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 ...
随机推荐
- 配置海康IPC或大华IPC通过路由器公网访问
设备:路由器DLink-DIR-600M,海康IPC:DS-2CD864FWD-E 海康默认端口为8000,HTTP访问为80,RTSP访问端口为554. 配置分成两步,分别为配置IPC相关网络参数和 ...
- WPF:鼠标长时间无操作,窗口隐藏
//设置鼠标长时间无操作计时器 private System.Timers.Timer MouseTimerTick = new System.Timers.Timer(10000); private ...
- Excel2Dataset
//获取用户打开的Excel文档路径 private stringkkk() { OpenFileDialog selectFile = new OpenFileDialog(); selectFil ...
- jquery-weui picker组件实现只选择年月
var date = new Date() var month = date.getMonth()+1 //获取当前月份 $('#selectTime').picker({ toolbarTempla ...
- HDU3577 线段树(区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 ,普通的线段树区间更新题目,较简单. 相当于一个区间覆盖问题,有一点要注意的就是叶子节点是一个长 ...
- shell脚本常识
--------------------------------------------------------------- -------------- 概要 -- ...
- linux下安装和卸载mysql
卸载: 1 . rpm -qa | grep -i mysql命令查看已经安装过的组件. 2. 使用yum -y remove命令卸载已经安装的MySQL组件,使用下面的命令,对于上面已经安装 ...
- iOS 真机报错 Command CodeSign failed with a nonzero exit code
今天在网上下载的一个小demo,在模拟器上正常运行,但是在真机上报错: 解决方式: 1.打开钥匙串:2.锁住login keychain: 3.再把它解锁. 然后就能真机上正常运行了,
- 2018.6.16 PHP小实验
PHP实验 实验一 <?php /** * Created by PhpStorm. * User: qichunlin * Date: 2018/5/17 * Time: 下午5:35 */ ...
- 输出流缓冲的意义 何时缓冲 Stdout Buffering
From : https://eklitzke.org/stdout-buffering 译者:李秋豪 大多数编程语言默认提供了i/o缓冲特性,因为这会使得输出更加有效率.这些缓冲功能大都是默默工作& ...