[LeetCode] 142. Linked List Cycle II

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

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.

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.

Constraints:

  • The number of the nodes in the list is in the range [0, 104].
  • -105 <= Node.val <= 105
  • pos is -1 or a valid index in the linked-list. 

Follow up: Can you solve it using O(1) (i.e. constant) memory?

单链表中的环二。

给定一个链表的头节点  head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

不允许修改 链表。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/linked-list-cycle-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题意是给一个链表,如果这个链表中有环,请return环的起点;若没有,return null。找是否有环可以参照[LeetCode] 141. Linked List Cycle的讲解。至于怎么找到环的起点,我这里引用一个非常好的讲解,https://www.cnblogs.com/hiddenfox/p/3408931.html

因为快慢指针的速度是一个2步一个1步,所以当两个指针相遇的时候,fast走过的长度一定是slow的两倍。两者相遇的地方一定是环的起点。至于证明,直接参照引用贴。

时间O(n)

空间O(1)

JavaScript实现

 1 /**
 2  * @param {ListNode} head
 3  * @return {ListNode}
 4  */
 5 var detectCycle = function(head) {
 6     // corner case
 7     if (head === null || head.next === null) {
 8         return null;
 9     }
10 
11     // normal case
12     let slow = head;
13     let fast = head;
14     while (fast !== null && fast.next !== null) {
15         slow = slow.next;
16         fast = fast.next.next;
17         if (fast === slow) {
18             let slow2 = head;
19             while (slow !== slow2) {
20                 slow = slow.next;
21                 slow2 = slow2.next;
22             }
23             return slow;
24         }
25     }
26     return null;
27 };

 

Java实现

 1 /**
 2  * Definition for singly-linked list.
 3  * class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode detectCycle(ListNode head) {
14         // corner case
15         if (head == null || head.next == null) {
16             return null;
17         }
18 
19         // normal case
20         ListNode slow = head;
21         ListNode fast = head;
22         while (fast != null && fast.next != null) {
23             slow = slow.next;
24             fast = fast.next.next;
25             if (slow == fast) {
26                 ListNode slow2 = head;
27                 while (slow != slow2) {
28                     slow = slow.next;
29                     slow2 = slow2.next;
30                 }
31                 return slow;
32             }
33         }
34         return null;
35     }
36 }

 

LeetCode 题目总结

posted @ 2019-11-10 05:24  CNoodle  阅读(451)  评论(0编辑  收藏  举报