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 ...
随机推荐
- 用Python写单向链表和双向链表
链表是一种数据结构,链表在循环遍历的时候效率不高,但是在插入和删除时优势比较大. 链表由一个个节点组成. 单向链表的节点分为两个部分:存储的对象和对下一个节点的引用.注意是指向下一个节点. 而双向链表 ...
- JAVA——遍历
关于遍历,发现了个坑. 详见如下: package com.fxl.test; import java.util.ArrayList; import java.util.Iterator; impor ...
- hdoj1087 (DP--LIS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 思路:这题很简单了,纯LIS的解法,没有一点变形,由于数据小,使用O(n^2)LIS解法就足够了 ...
- STL容器的常用用法
STL: 1.vector: vector<int> v;vector<int> v(10);//定义大小为10的int型向量容器.vector<int> v(10 ...
- c++流操作
非缓冲标准出错流对象cerr和缓冲标准出错流对象clog,它们都是来自于ostream类的对象,用于输出错信息.cerr和clog之间的不同之处在于cerr是不经过缓冲区直接向显示器输出有关信息,而c ...
- 13-前端不通路径同一个请求访问同一个页面时,有时样式没有加载出来(jss,image,css)
通过如下方式访问同一个网站时,下面一个可以加载样式,而下面一个加载的页面却没有样式,思考良久没有想通,当时也忘记了用浏览器看下 css,js,image的请求路径,其实在前端页面里面我直接: 这样引 ...
- SSH登录到远程linux机器并执行命令
一. 1.JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成 ...
- Python和JavaScript间代码转换4个工具-乾颐堂
Python 还是 JavaScript?虽然不少朋友还在争论二者目前谁更强势.谁又拥有着更为光明的发展前景,但毫无疑问,二者的竞争在 Web 前端领域已经拥有明确的答案.立足于浏览器平台,如果放弃 ...
- 04 Maven 仓库
Maven 仓库 在 Maven 坐标与依赖 中详细介绍了 Maven 坐标和依赖,坐标和依赖是任何一个构件在 Maven 世界中的逻辑表示方式:而构件的物理表示方式是文件, Maven 通过仓库来统 ...
- springmvc 测试项目示例
新建一个 maven项目 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...