[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?
- 使用快慢指针,慢指针每次前进一步,快指针每次两步
- 如果快慢指针相遇了,那么将快指针从标记带链表头,改为每次前进一步
- 当快慢指针再次相遇便是环起始位置。
这样的实现,时间很快O(n),而且空间O(1)
#include <iostream>
using namespace std;
/**
* 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==NULL) return NULL;
ListNode * fast=head,*slow=head;
while(){
if(fast->next!=NULL) fast=fast->next;
else return NULL;
if(fast->next!=NULL) fast=fast->next;
else return NULL;
slow=slow->next;
if(fast==slow) break;
}
fast=head;
while(){
if(fast==slow) return slow;
fast=fast->next;
slow=slow->next;
}
return NULL;
}
}; int main()
{
ListNode node1(),node2(),node3(),node4(),node5();
node1.next=&node2;
node2.next=&node3;
node3.next=&node4;
node4.next=&node5;
node5.next=&node1;
Solution sol;
ListNode *ret = sol.detectCycle(&node1);
if(ret==NULL) cout<<"NULL"<<endl;
else cout<<ret->val<<endl;
return ;
}
[LeetCode] Linked List Cycle II 链表环起始位置的更多相关文章
- LeetCode Linked List Cycle 单链表环
题意:给一个单链表,判断其是否出现环! 思路:搞两个指针,每次,一个走两步,另一个走一步.若有环,他们会相遇,若无环,走两步的指针必定会先遇到NULL. /** * Definition for si ...
- [LeetCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [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, returnnull. Follo ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- LeetCode Linked List Cycle II 单链表环2 (找循环起点)
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...
随机推荐
- 认识/etc/passwd和/etc/shadow
认识/etc/passwd和/etc/shadow============================== /etc/passwd [root@aminglinux ~]# head -n1 /e ...
- mysql 5.7初始化默认密码错误
下载了一个mysql 5.7.17的安装包后,安装后怎么都启动不了,好在mysql安装是成功了,没办法只有使用命令行重新初始化设置了 我的mysql安装根目录为:C:\Program Files\My ...
- myeclipse10.5 crack(2012-12-27-bd 写的日志迁移
首先去网上下一个破解文件如图: 解压过后打开的文件夹如图: 再打开crack文件夹如图: 运行run.bat如果点击它没反应就是你没有安装jdk,它如果运行就如图所示: 到这一步就在第一个方框user ...
- 使用virtualbox安装的Ubuntu,窗口分辨率过小,使用增强工具完成和vmtools一样的功能。
今天用VirtualBox成功装上Ubuntu10.04之后发现了一个问题:默认情况下 ubuntu 的分辨率最高只能设到800*600.但是对于自己的大显示器,在分辨率800*600的ubuntu窗 ...
- Flow Problem HDU - 3549
Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...
- utf8和utf8mb4区别
原文链接 一.简介 MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode.好在utf8mb4是utf8的超集,除了将编 ...
- Cuba studio框架中使用thymeteaf模板时中文乱码
最近公司换了Cuba这个orm框架,框架中使用了thymeteaf模板技术,发现在html中解析汉字一直是乱码的存在 一直以为是tomcat的问题但是tomcat的server.xml,项目中的web ...
- golang echo livereload
echo on port 1323 gin -a 1323 run server.go go get github.com/codegangsta/gin gin -h
- 3282: Tree(LCT)
3282: Tree Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 2249 Solved: 1042[Submit][Status][Discus ...
- 非负矩阵分解(NMF)原理及算法实现
一.矩阵分解回想 矩阵分解是指将一个矩阵分解成两个或者多个矩阵的乘积.对于上述的用户-商品(评分矩阵),记为能够将其分解为两个或者多个矩阵的乘积,如果分解成两个矩阵和 .我们要使得矩阵和 的乘积能够还 ...