Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Note: Do not modify the linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.

Follow up:
Can you solve it without using extra space?

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

这个题是不仅是判断链表中是否存在环,还要返回环的开始的位置。

用双指针,与141. Linked List Cycle 的类似,关于怎么返回这个位置,可以参考这个大佬的博客:http://www.cnblogs.com/hiddenfox/p/3408931.html

C++代码:

/**
* 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) {
ListNode *slow,*fast;
slow = head;
fast = head;
while(true){
if(fast == NULL || fast->next == NULL)
return NULL;
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
break;
}
slow = head;
while(slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
};

(链表 双指针) leetcode 142. Linked List Cycle II的更多相关文章

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

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

  3. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

  4. leetcode 142. Linked List Cycle II 环形链表 II

    一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点  head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...

  5. (链表 双指针) leetcode 141. Linked List Cycle

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  6. leetcode 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...

  7. leetcode 142. Linked List Cycle II ----- java

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...

  8. LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

  9. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

随机推荐

  1. 一、linux扩展

    1.linux-解压bz2文件提示tar (child): bzip2: Cannot exec: No such file or directory 原因,linux下没有bzip2解压工具 安装b ...

  2. sql中的begin catch 。。。end catch 的用法

    begin catch ...end  catch 是用来处理异常的 begintry--SQLendtry begincatch--sql (处理出错动作)endcatch 我们将可能会出错的sql ...

  3. Oracle 查询两个时间段内的所有日期列表

    1.查询某时间段内日期列表 select level,to_char(to_date('2013-12-31','yyyy-mm-dd')+level-1,'yyyy-mm-dd') as date_ ...

  4. java split(regex,limit) 使用记录

    1.split(“,”,0):是切割默认模式等同于split(",")结尾符合分割字符为空不进行进行分割,如图: 2.split(",",-1):limit参数 ...

  5. 今日头条移动app广告激活数据API对接完整Java代码实现供大家参考》》》项目随记

    这是自毕业后的第一篇博客,希望自己今后能养成写博客的一个好习惯.最近公司为了加速APP推广,采取在外部平台(如:今日头条)进行广告投放的方式,进行用户引流.因此我们需要对广告的激活数据进行一个检测,跟 ...

  6. Introduction to Dynamic SQL

    The idea of using dynamic SQL is to execute SQL that will potentially generate and execute another S ...

  7. 提高网络灵活性和效率的组网方式—SD-WAN

    导读 最初,大多数企业只是简单地将软件覆盖添加到广域网连接上.但是,随着时间的推移,由于SD-WAN架构的易配置功能,企业将开始采用SD-WAN更复杂的功能. 广域网一般用于连接多个业务地点,如总部和 ...

  8. BZOJ1324Exca王者之剑&BZOJ1475方格取数——二分图最大独立集

    题目描述   输入 第一行给出数字N,M代表行列数.N,M均小于等于100 下面N行M列用于描述数字矩阵 输出 输出最多可以拿到多少块宝石 样例输入 2 2 1 2 2 1 样例输出 4   题意就是 ...

  9. Qt Creator 中文编译失败 怎么办

    在Qt Creator 中c++源码有中文字符,结果不能编译成功. 代码 QMessageBox::warning(this, "警告","用户名密码错误",Q ...

  10. 搜索引擎(Elasticsearch搜索详解)

    学完本课题,你应达成如下目标: 掌握ES搜索API的规则.用法. 掌握各种查询用法 搜索API 搜索API 端点地址 GET /twitter/_search?q=user:kimchy GET /t ...