题目:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. [算法][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 ...

  5. 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 ...

  6. 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 ...

  7. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  8. 【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 ...

  9. [Linked List]Linked List Cycle,Linked List Cycle II

    一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...

随机推荐

  1. 解决Lightmap在PC上与ios和Android上表现不同的问题

    Lightmap在PC上与android和ios的区别以及解决方法 1.  问题描述 相信很多人碰到过Lightmap的一些问题: 烘培好Lightmap之后,在PC上看起来相当给力,而打包成ios或 ...

  2. DOS中符号的英文对照

    --------------siwuxie095 1..:point 或 dot 或 period 或 full stop 2.空格:space 或 blank ------------------- ...

  3. GridView中如何实现自定义时间货币等字符串格式?

    方法一: <asp :GridView ID="GridView1" runat="server"> <columns> <asp ...

  4. shell中数组基础语法

    数组的基本赋值 arr=(a b c) arr[index]=a 2.常用的两个方法 str=${arr[@]}(数组转化成字符串) len=${#arr[*]}(数组长度) 3.遍历数组的方法 #! ...

  5. Halcon的编程语法与数据处理——第8讲

    1.跟其他语言不完全一致的表达符号 赋值符号  := 引号      ' ' (一律是单引号) 求商求余  /   % (一个整数除以另一个数,如何使商是实型的?即浮点型) 逻辑运算  and  or ...

  6. go实现的简易TCP的客户端和服务器

    今天介绍golang版本的通信基础:基于TCP的客户端和服务器实现,参考书籍:The Way To Go 那时学习java的时候也是做过通信的,当时是socket编程,服务器监听某一个端口,然后客户机 ...

  7. C语言文本处理

    一.conf文本 http://blog.163.com/lixiangqiu_9202/blog/static/53575037201431743236762/ http://blog.csdn.n ...

  8. VK Cup 2016 D. Bear and Two Paths 模拟

    D. Bear and Two Paths time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  9. [BAT]cmd命令之 cd /d %~dp0

    cd /d %~dp0是什么意思啊?批处理文件中的一条语句意思是 更改当前目录为批处理本身的目录 有些晕吧?不急,我举例 比如你有个批处理a.bat在D:\qq文件夹下 a.bat内容为 cd /d ...

  10. 2、C++

    2.2定义变量 2.2.1命名规则 赋予变量的名称叫做标识符,或者更方便地称之为变量名.变量名可用字母(包括大小写),数字,以及下划线,其他字符不允许.以下划线或者字母开头.在Visual C++20 ...