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?

141. Linked List Cycle 的拓展,这题要返回环开始的节点,如果没有环返回null。

解法:双指针,还是用快慢两个指针,相遇时记下节点。参考:willduan的博客

Java:

public class Solution {
public ListNode detectCycle( ListNode head ) {
if( head == null || head.next == null ){
return null;
}
// 快指针fp和慢指针sp,
ListNode fp = head, sp = head;
while( fp != null && fp.next != null){
sp = sp.next;
fp = fp.next.next;
//此处应该用fp == sp ,而不能用fp.equals(sp) 因为链表为1 2 的时候容易
//抛出异常
if( fp == sp ){ //说明有环
break;
}
}
//System.out.println( fp.val + " "+ sp.val );
if( fp == null || fp.next == null ){
return null;
}
//说明有环,求环的起始节点
sp = head;
while( fp != sp ){
sp = sp.next;
fp = fp.next;
}
return sp;
}
}   

Python:

class ListNode:
def __init__(self, x):
self.val = x
self.next = None def __str__(self):
if self:
return "{}".format(self.val)
else:
return None class Solution:
# @param head, a ListNode
# @return a list node
def detectCycle(self, head):
fast, slow = head, head
while fast and fast.next:
fast, slow = fast.next.next, slow.next
if fast is slow:
fast = head
while fast is not slow:
fast, slow = fast.next, slow.next
return fast
return None  

C++:

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) break;
}
if (!fast || !fast->next) return NULL;
slow = head;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return fast;
}
};

  

类似题目:

[LeetCode] 141. Linked List Cycle 链表中的环

All LeetCode Questions List 题目汇总

[LeetCode] 142. Linked List Cycle II 链表中的环 II的更多相关文章

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

  2. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

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

  4. LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java

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

  5. [LintCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...

  6. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  7. [leetcode]141. Linked List Cycle判断链表是否有环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

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

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

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

随机推荐

  1. Python应用-完成简单邮件发送功能

    项目中有时候需要用脚本来自动发送邮件,而用Python来发送邮件十分的方便,代码如下: #!/usr/bin/python #coding:utf-8 import smtplib from emai ...

  2. CodeForces - 76A:Gift (最小生成树 解决单调性问题是思想)

    题意:给定N点M边的无向连通图,每条边有两个权值(g,s). 给定G,S. 让你给出一组(g0,s0)使得图中仅留下g<=g0, s<=s0的边之后,依然连通,并求Gg0+Ss0的最小值. ...

  3. Spring源码窥探之:@Profile

    Spring为我们提供的多环境启动 1. 配置类,注入三个不同环境的数据源,并加上注解 /** * description: 以下准备了三套不同环境的数据源 * * @author 70KG * @d ...

  4. 韩顺平老师java视频全套-java视频教程下载

    解压压缩包会有一个种子文件.直接迅雷下载即可,包含了韩顺平老师的java入门视频,jdbc,jsp,servlet,oracle,hibermate,spring,SHH框架,struct,linux ...

  5. django-用户中心订单页面

    提交订单页面place_order.html,创建订单成功后跳转到用户订单页面 {% block bottomfiles %} <script type="text/javascrip ...

  6. 使用idea 调试java -jar xxx.jar方式启动

    今日思语:希望是什么?希望就是 你还在挣扎中... idea是一个功能强大的java开发工具,可以很方便的帮助开发人员进行开发工作. 1.有时我们通过使用java -jar xxx.jar方式启动可执 ...

  7. LeetCode 1059. All Paths from Source Lead to Destination

    原题链接在这里:https://leetcode.com/problems/all-paths-from-source-lead-to-destination/ 题目: Given the edges ...

  8. Lightning Web Components 安装试用(一)

    Lightning Web Components 简称(lwc) 是一个快速企业级的web 组件化解决方案,同时官方文档很全,我们可以完整的 学习lwc 项目结构 使用npx 官方提供了一个creat ...

  9. PHP生成随机数;订单号唯一

    //8-12位随机数 function makeRand($num=){ $strand = (; if(strlen($strand)<$num){ $strand = str_pad($st ...

  10. 【POJ2251】Dungeon Master

    本题传送门 本题知识点:宽度优先搜索 题意简单.在一个L层高的楼里,去走迷宫,就是问从S走到E的最短路径.每走一格每上或者下一层都算1步. 一开始以为这个"立体迷宫"有点吓到我(题 ...