|
| 1 | +# 环形链表2 |
| 2 | + |
| 3 | +#### *Linked List Cycle II* |
| 4 | + |
| 5 | +给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。 |
| 6 | + |
| 7 | +为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。 |
| 8 | + |
| 9 | +说明:不允许修改给定的链表。 |
| 10 | + |
| 11 | +进阶: |
| 12 | + |
| 13 | +你是否可以使用 O(1) 空间解决此题? |
| 14 | + |
| 15 | + |
| 16 | +英文题目: |
| 17 | + |
| 18 | +Given the `head` of a linked list, return the node where the cycle begins. If there is no cycle, return null. |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +Do not modify the linked list. |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +**example 1** |
| 27 | +/circularlinkedlist.png) |
| 28 | + |
| 29 | +``` |
| 30 | +Input: head = [3,2,0,-4], pos = 1 |
| 31 | +Output: true |
| 32 | +Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed). |
| 33 | + |
| 34 | +``` |
| 35 | + |
| 36 | + |
| 37 | +**example 2** |
| 38 | +/circularlinkedlist_test2.png) |
| 39 | + |
| 40 | +``` |
| 41 | +Input: head = [1,2], pos = 0 |
| 42 | +Output: tail connects to node index 0 |
| 43 | +Explanation: There is a cycle in the linked list, where tail connects to the first node. |
| 44 | + |
| 45 | +``` |
| 46 | + |
| 47 | +**example 3** |
| 48 | +/circularlinkedlist_test3.png) |
| 49 | + |
| 50 | +``` |
| 51 | +Input: head = [1], pos = -1 |
| 52 | +Output: no cycle |
| 53 | +Explanation: There is no cycle in the linked list. |
| 54 | + |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +**Constraints:** |
| 59 | + |
| 60 | +The number of the nodes in the list is in the range [0, 104]. |
| 61 | + |
| 62 | +-105 <= Node.val <= 105 |
| 63 | + |
| 64 | +pos is -1 or a **valid index** in the linked-list. |
| 65 | + |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +### 思路 |
| 70 | + |
| 71 | +1. 参照[Linked List Cycle](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/60.%20Linked%20List%20Cycle(Easy))可判断链表是否有环 |
| 72 | +2. 若链表有环,在相遇处让其中一个指针指向头节点`head`后,两个指针同时前进,再次相遇的节点即为环的起始节点 |
| 73 | +3. 具体原理如下图 |
| 74 | +/2.jpeg) |
| 75 | + |
| 76 | + |
| 77 | +### 代码 |
| 78 | +``` |
| 79 | + |
| 80 | +/* |
| 81 | + * @lc app=leetcode id=142 lang=cpp |
| 82 | + * |
| 83 | + * [142] Linked List Cycle II |
| 84 | + */ |
| 85 | + |
| 86 | +// @lc code=start |
| 87 | +/** |
| 88 | + * Definition for singly-linked list. |
| 89 | + * struct ListNode { |
| 90 | + * int val; |
| 91 | + * ListNode *next; |
| 92 | + * ListNode(int x) : val(x), next(NULL) {} |
| 93 | + * }; |
| 94 | + */ |
| 95 | +class Solution { |
| 96 | +public: |
| 97 | + ListNode *detectCycle(ListNode *head) { |
| 98 | + if (head == nullptr) |
| 99 | + return nullptr; |
| 100 | + ListNode *fast, *slow; |
| 101 | + fast = slow = head; |
| 102 | + while (fast->next != nullptr && fast->next->next != nullptr) |
| 103 | + { |
| 104 | + fast = fast->next->next; |
| 105 | + slow = slow->next; |
| 106 | + // 有环 |
| 107 | + if (fast == slow) |
| 108 | + { |
| 109 | + // slow 从head从新走,相遇即为环起点 |
| 110 | + slow = head; |
| 111 | + while (fast != slow) |
| 112 | + { |
| 113 | + fast = fast->next; |
| 114 | + slow = slow->next; |
| 115 | + } |
| 116 | + return fast; |
| 117 | + } |
| 118 | + } |
| 119 | + return nullptr; |
| 120 | + } |
| 121 | +}; |
| 122 | +// @lc code=end |
| 123 | + |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +本题以及其它leetcode题目代码github地址: [github地址](https:github.com/SherlockUnknowEn/leetcode) |
0 commit comments