- 浏览: 184893 次
- 性别:
- 来自: 济南
文章分类
最新评论
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?
判断一个链表中是否有环存在,并且输出环的起始点。我们用快慢指针fast,slow来判断是否有环存在,如果它们可以相遇就说明有环存在,如果不能相遇说明没有环存在。它们相遇之后,图中的meet点是它们的相遇点,假设join为环的起始点,我们假设head到join的距离为a, join到meet的距离为b(逆时针),meet到join的距离为c(逆时针)。如果它们相遇,那么fast走过的路程是slow走过的两倍,所以a + b + c + b = 2 ( a + b),即a = c。有了这个关系我们只需要用两个指针一个从相遇点出发,另一个从头结点出发,它们相遇的点就是环的起始点。代码如下:
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
判断一个链表中是否有环存在,并且输出环的起始点。我们用快慢指针fast,slow来判断是否有环存在,如果它们可以相遇就说明有环存在,如果不能相遇说明没有环存在。它们相遇之后,图中的meet点是它们的相遇点,假设join为环的起始点,我们假设head到join的距离为a, join到meet的距离为b(逆时针),meet到join的距离为c(逆时针)。如果它们相遇,那么fast走过的路程是slow走过的两倍,所以a + b + c + b = 2 ( a + b),即a = c。有了这个关系我们只需要用两个指针一个从相遇点出发,另一个从头结点出发,它们相遇的点就是环的起始点。代码如下:
/** * 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; ListNode slow = head; while(fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if(slow == fast) break; } if(fast != slow) return null; fast = head; while(fast != slow) { fast = fast.next; slow = slow.next; } return fast; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 389Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 668Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 434Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 935Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 691Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 857Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For a undirected graph with tre ...
相关推荐
python python_leetcode题解之142_Linked_List_Cycle_II
javascript js_leetcode题解之142-linked-list-cycle-ii.js
4. 链表环检测 II(Linked List Cycle II, 如LeetCode的第142题):找到链表环内的起点。 5. K 个一组翻转链表(Reverse Nodes in K-Group, 如LeetCode的第25题):将链表中的节点每K个一组进行反转。 解决这些题目...
List LeetCode 92.Reverse Linked List II LeetCode 138.Copy List with Random Pointer LeetCode 142.Linked List Cycle II(solve1) LeetCode 142.Linked List Cycle II(solve2) LeetCode 160.Intersection of Two ...
Linked List Cycle II**:找到链表环的长度,并给出进入环的第一个节点。 4. **解决链表问题的策略** - **迭代法**:使用循环遍历链表,通常适用于大多数链表操作。 - **递归法**:对于某些特定问题,如链表反转...
15. **Linked List Cycle II**:找到链表中的环的起始节点。通过修改快慢指针的速度,可以确保它们在环内相遇并能确定环的起点。 16. **Reverse Linked List**:反转链表。可以使用迭代或递归的方式,改变节点间的...
java lru leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest ...II ...141.Linked List Cycle 142.Linked List Cycle II ...II
python python_leetcode题解之141_Linked_List_Cycle
javascript js_leetcode题解之141-linked-list-cycle.js
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 ...
6. **环形链表**(Linked List Cycle II) - 知识点:链表,快慢指针法(Floyd判圈法) - 解题方法:设置两个指针,一个速度慢,每次移动一步,一个速度快,每次移动两步。如果链表存在环,它们会在环内相遇。 7....
* [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...
II 快慢指针再加个初始指针 慢指针到链表开始位置时, 快指针总是比他快k步(每次移动快1步, 移动k次), 第一次相遇的时候快慢指针位置距离链表起始位置差k步即在n-k的位置(链表环长度为n) Majority Element 多数投票...
8. 链表的环形检测(Detect Cycle in Linked List) 链表的环形检测是检测链表中是否存在环形结构。这种操作可以用于解决一些特殊的问题,例如,检测链表中的环形结构以避免死循环。 知识点:链表的环形检测的算法...
leetcode 答案Leet Code 挑战 这是我提交给 Lambda School CS Unit 2 构建周的已接受 ...如果您想自己尝试,这些链接将带您进入代码挑战。...要查看我接受的答案,只需转到与...[Linked List Cycle II](https://leetcode.co
leetcode 答案leetcode-java ...的 Java ...II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree Balanced Binary Tree Maximum Depth of Binary Tree Same Tree
Linked List Linked List Cycle Given a linked list, determine if it has a cycle in it. public static boolean hasCycle(ListNode head) 快慢指针法,块指针从head.next开始,慢指针从head开始,快指针每次移动...
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