【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 ...
随机推荐
- C# 获取当前文件、文件夹的路径及操作环境变量
一.获取当前文件的路径 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName 获取模块的完整路径,包 ...
- python基础-三元运算和bytes数据
三元运算 进制 二进制,01 八进制,01234567 十进制,0123456789 十六进制,0123456789ABCDEF bytes类型 http://www.cnblogs. ...
- jquery实现加载更多效果
情况是当滑动条滑动到最底部的时候,数据显示出一部分的更多 思路:获取到浏览器屏幕的高度client,文档的高度h和滑动距离顶部的距离scroll,当h<=client+scroll的时候就是滑动 ...
- UOJ#130 【NOI2015】荷马史诗 K叉哈夫曼树
[NOI2015]荷马史诗 链接:http://uoj.ac/problem/130 因为不能有前缀关系,所以单词均为叶子节点,就是K叉哈夫曼树.第一问直接求解,第二问即第二关键字为树的高度. #in ...
- CAsyncSocket create创建套接字失败
解决方法: 在继承CAsyncSocket 类的子类的构造函数中加入以下代码: if (!AfxSocketInit()) AfxMessageBox(IDP_SOCKETS_INIT_FAILED) ...
- 使用JDBC操作SAP云平台上的HANA数据库
本文假设您对JDBC(Java Database Connectivity)有最基本的了解.您也可以将其同ADBC(ABAP Database Connectivity)做对比,细节请参考我的博客AD ...
- java程序换图标
ImageIcon img = new ImageIcon("D:\\mahou-in-action\\ShiJuanFenXi\\src\\zoom-in.png"); inst ...
- SAP 日志管理
现在项目上自开发的dialog程序越来越多,有很多敏感数据需要像SAP标准的业务一样,能看到所有的修改日志,要想实现日志的功能,有以下几个办法: 办法一.建一个日志表,在原有表的基础上,加上日期和时间 ...
- JS - OOP-继承的最佳实现方式
如上图,使用第三种方式实现继承最好,也就是加了下划线的. 但是Object.create方法是ES6才支持的,所以,右边就写了一个实现其同样功能的函数.
- ZendFramework-2.4 源代码 - ViewManager类图