`
frank-liu
  • 浏览: 1666005 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: Linked List Cycle II

 
阅读更多

问题描述:

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

Note: Do not modify the linked list.

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

原问题链接:https://leetcode.com/problems/linked-list-cycle-ii/

 

问题分析

  和前一个问题比起来,这个问题相对于前面一个问题更进一步。在这里不但要看是否存在有环,而且还要在有环的情况下判断那个存在环的入口在哪里。我们这里先讨论存在环的情况,对于一个单链表来说,它存在环的话只可能是类似于如下的形式:

  假设从链表的开头到这个环的起始点的距离是a,而环的长度是b。在按照前面采用快慢指针的方式遍历环的时候,它们必然会在环的某个点相遇,假设这个点离环的起始点的距离为x。那么按照前面的假设,快指针肯定是走完了整个链表并加上x的这一段。它走过的距离应该是fast = a + nb + x(其中n表示快指针可能走过的环的圈数,n >= 1),而慢指针走过的距离是a + x,也就是说slow = a + x。同时,按照速度来说,快指针是慢指针的两倍,于是有 fast = 2 x slow。也就是说有  a + nb + x = 2a + 2x。

  这样就有a = nb - x。也就是说,从链表的起始点到这个环的入口就是从两个指针相遇的点到环的起点再加上若干个环的距离,这里可能是0个或者多个环的距离。那么,从代码实现的角度来看,它里面不管走多少个环的距离,在环里头相当于还是回到了它原来的地方。而在环里头距离环入口x的位置再走个n -x的距离,它不就正好走到环的入口点了么?

  这就是问题的关键所在,从链表的入口走到环的入口和从环里头两个相遇的节点的位置不断往前走的时候,它们会在环的入口位置相遇。所以,我们在实现的时候首先找到两个指针相遇的地方。然后再从链表的开头走一个指针过来,和环里面那个slow指针一起走。一旦它们相遇,那么那个点就是环的入口了。

  详细代码实现如下:

 

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        if(head == null || head.next == null) return null;
        ListNode fast = head, slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) break;
        }
        if(fast == null || fast.next == null) return null;
        slow = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}

 

  • 大小: 11 KB
分享到:
评论

相关推荐

    leetcode中文版-LeetCode:力码

    leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...

    lrucacheleetcode-LeetCode:LeetCode刷题

    II(Linked List Cycle II) 2018.9.26 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II) 2018.9.27 重建二叉树(Rebuild Binary Tree) 2018.9.28 把字符串转换成整数(Convert a string to ...

    leetcode中325题python-leetcode:leetcode

    leetcode中325题python leetcode 以 参考 和 Hash相关 1_两数之和 ...linked-list-cycle-ii 143 重排链表 reorder-list 148 排序链表 sort-list 234 回文链表 palindrome-linked-list 双指针遍历/滑动

    leetcode2sumc-LeetCode:LeetCode的一些题目

    leetcode 2 sum c LeetCode 帮助文档 ...Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List

    lrucacheleetcode-leetcode:leetcode

    Linked_list_cycle.py: long_pressed_name.py: max_69_number.py: max_array_sum_after_negations.py: max_depth_n_ary_tree.py: max_string_split_score.py: max_sub_array.py: merge_2_trees.py: plus_one...

    LeetCode:LeetCode解决方案

    preorder-traversal链表reorder-list链表linked-list-cycle-ii链表linked-list-cycle动态规划word-break-ii动态规划word-break链表copy-list-with-random-pointer复杂度single-number-ii复杂度single-number动态规划

    leetcode卡-leetcode:利特码解决方案

    II Valid Sudoku linked list Palindrome linked list Linked List Cycle trees Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set ...

    leetcode中国-leetcode:刷算法了

    II 快慢指针再加个初始指针 慢指针到链表开始位置时, 快指针总是比他快k步(每次移动快1步, 移动k次), 第一次相遇的时候快慢指针位置距离链表起始位置差k步即在n-k的位置(链表环长度为n) Majority Element 多数投票...

    leetcode添加元素使和等于-leetcode:力码

    leetcode添加元素使和等于 总结 按照类别分类来刷 刷当前题的时候,看下『题目描述』最底下有个『相似题目』,这些题的思路...linked-list-cycle-ii reverse-nodes-in-k-group 二叉树 实现一个二叉树 二叉树二叉树的

    leetcode怎么销号-LeetCode-Top-Interview-Questions:LeetCode-Top-Interview-Qu

    leetcode怎么销号 LeetCode便签 Linked List Cycle 问题描述 Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解决思路 声明一个慢指针和一个快...

    leetcode答案-leetcode-java:leetcode的Java代码

    leetcode 答案leetcode-java leetcode.com ...II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree Balanced Binary Tree Maximum Depth of Binary Tree Same Tree

    leetcode不会-Leetcode-Java:Leetcode-Java

    leetcode 不会 Leetcode Solutions in Java Linked List Linked List Cycle Given a linked list, determine if it has a cycle in it. public static boolean hasCycle(ListNode head) 快慢指针法,块指针从head....

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-linked-lists 206-反转一个单链表:reverse-linked-list 20-有效的括号:valid-parentheses 150-逆波兰表达式求值:evaluate-reverse-polish-...

    javalruleetcode-leetcode-java:力码笔记

    leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted ...

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    leetcode有效期-Datawhale-Coding:Datawhale-编码

    leetcode有效期 Datawhale-Coding All Tasks Task 1:数组和链表(2天) 时间:2019-08-03 21:00 - 2019-08-05 ...学习哈希表思想,并完成leetcode上的两数...Linked List Cycle I(环形链表) 英文版: 中文版: Merge k

    leetcode答案-LeetCodeSolution:这是LeetCode网站问题的解决方案集

    Linked List Cycle 判断链表是否有环。通过快慢节点可以简单实现。 Unique Binary Search Trees 本题参考了 里面的*How Many Binary Trees Are There?*章节。 The first few terms: C(0) = 1 C(1) = C(0)C(0) = 1 C...

    leetcode2sumc-DataWhale_exercise:用C++编程

    leetcode 2 sum c DataWhale_exercise programming in c++ Task 1:数组和链表(2天)【当前任务】 时间:2019-08-03 ...学习哈希表思想,并完成leetcode上的两数之和(1)及Happy ...Linked List Cycle I(环形链

    leetcode和oj-leetcode_downloader:用于您已接受的leetcodeoj提交的下载器

    linked-list-cycle/Solution.993783.java $ tree . ├── add-binary │  └── Solution.665166.java ├── add-two-numbers │  └── Solution.666385.java ├── balanced-binary-tree │  └── ...

    leetcode答案-code_challenges:代码挑战

    leetcode 答案Leet Code 挑战 这是我提交给 Lambda School CS Unit 2 构建周的已接受 ...如果您想自己尝试,这些链接将带您进入代码挑战。...要查看我接受的答案,只需转到与代码挑战名称匹配的...II](https://leetcode.co

Global site tag (gtag.js) - Google Analytics