LeetCode142: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,所以一定会相遇,而不会出现快指针直接跳过慢指针的情况。
如何找到环的入口点呢?
我们先看图再说话:

从图各段我们分析,因为quick指针每次走两步二slow指针每次走一步,所以当两指针相遇时,quick走了两倍的slow指针所走长度即:假设相遇点为z点
a + b + n * ( b + c ) = 2 * (a + b) 公式1
整理得:
a = n * (b + c) – b 公式2
根据公式2可知,要找到环入口点,可使用两个指针,p1和p2,p1从链表头开始走,p2从z点即快慢指针相遇点开始走,当p1指针走到Y(环入口点)时即长度为a时,p1走了n * (b + c) – b,可知p1也正好在Y点,所以利用p1和p2两指针,当它们相遇时,相遇点即为环入口点。
实现代码:
#include <iostream>
using namespace std; /**
Linked List Cycle II
*/ struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
void addNode(ListNode* &head, int val)
{
ListNode *node = new ListNode(val);
if(head == NULL)
{
head = node;
}
else
{
node->next = head;
head = node;
}
}
void printList(ListNode *head)
{
while(head)
{
cout<<head->val<<" ";
head = head->next;
}
} class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL || head->next == NULL)
return NULL;
ListNode *quick = head;
ListNode *slow = head;
while(quick && quick->next)//利用快慢指针判断有无环
{
quick = quick->next->next;
slow = slow->next;
if(quick == slow)
break;
}
if(quick != slow)
return NULL;
//slow指针从头开始走,quick指针从相遇点开始走,根据公式可知,相遇点即为环入口点
slow = head;
while(slow != quick)
{
slow = slow->next;
quick = quick->next;
}
return slow;
}
};
int main(void)
{
ListNode *head = new ListNode();
ListNode *node1 = new ListNode();
ListNode *node2 = new ListNode();
ListNode *node3 = new ListNode();
head->next = node1;
node1->next = node2;
node2->next = node3;
node3->next = node1; Solution solution;
ListNode *rNode = solution.detectCycle(head);
if(rNode)
cout<<rNode->val<<endl; return ;
}
LeetCode142:Linked List Cycle II的更多相关文章
- LeetCode OJ:Linked List Cycle II(循环链表II)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- LeetCode之“链表”:Linked List Cycle && Linked List Cycle II
1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...
- LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II
链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- [算法][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 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 【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 ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
随机推荐
- 两个onCreate方法?你真的了解onCreate()么?
Activity的onCreate方法一直是我们编写一个activity最先重载的方法.细心的小伙伴在编写代码的时候回看到这样一幕: 咦,这里怎么会有两个onCreate提供给我们重载?选择困难症患者 ...
- 2.Add Two Numbers (List)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 转)安装svn服务器
以下转载自:http://www.linuxidc.com/Linux/2015-01/111956.htm 安装 安装软件包: sudo apt-get install subversion 配置 ...
- 一个性能较好的JVM参数配置
一个性能较好的web服务器jvm参数配置: -server//服务器模式-Xmx2g //JVM最大允许分配的堆内存,按需分配-Xms2g //JVM初始分配的堆内存,一般和Xmx配置成一样以避免每次 ...
- Web站点性能拨测脚本
功能:检测自己本地访问目标网站的返回状态.访问质量信息 [root@localhost src]# cat get_site_status.sh #! /usr/bin/env bash if [[ ...
- MQ基础概念和介绍
一.中间件 MQ是一种中间件产品,至于什么是中间件,中间件能干什么,参见以下链接: http://baike.baidu.com/view/23710.htm 二.WebSphere MQ的原理 We ...
- MVC仓储类Repository
接口: using Common; using System; using System.Collections; using System.Collections.Generic; using Sy ...
- jdk8 jvm配置参数说明
这些选项是特定于Java HotSpot虚拟机的通用选项.-X 显示所有可用-X选项的帮助. -Xbatch 禁用后台编译.默认情况下,JVM将该方法编译为后台任务,以解释器模式运行该方法,直到后台编 ...
- php初中高阶段
多学习PHP的朋友比较关心的问题之一就是学成后就业的薪资问题,关于PHP就业工资我们共同来探讨一下,其实小编觉得,PHP就业工资是多少并不重要,总要的是你的技术值多少钱,只要你的技术很棒,高薪根本就不 ...
- Mysql的内存优化
老师 vi mysqld_safe# executing mysqld_safe 后面增加export LD_PRELOAD=/usr/local/lib/libtcmalloc.so 可以做一 ...