|
| 1 | +<h2><a href="https://leetcode.com/problems/linked-list-cycle-ii/">142. Linked List Cycle II</a></h2><h3>Medium</h3><hr><div><p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p> |
| 2 | + |
| 3 | +<p>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 <code>next</code> pointer. Internally, <code>pos</code> is used to denote the index of the node that tail's <code>next</code> pointer is connected to (<strong>0-indexed</strong>). It is <code>-1</code> if there is no cycle. <strong>Note that</strong> <code>pos</code> <strong>is not passed as a parameter</strong>.</p> |
| 4 | + |
| 5 | +<p><strong>Do not modify</strong> the linked list.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong>Example 1:</strong></p> |
| 9 | +<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png" style="height: 145px; width: 450px;"> |
| 10 | +<pre><strong>Input:</strong> head = [3,2,0,-4], pos = 1 |
| 11 | +<strong>Output:</strong> tail connects to node index 1 |
| 12 | +<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the second node. |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p><strong>Example 2:</strong></p> |
| 16 | +<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png" style="height: 105px; width: 201px;"> |
| 17 | +<pre><strong>Input:</strong> head = [1,2], pos = 0 |
| 18 | +<strong>Output:</strong> tail connects to node index 0 |
| 19 | +<strong>Explanation:</strong> There is a cycle in the linked list, where tail connects to the first node. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong>Example 3:</strong></p> |
| 23 | +<img alt="" src="https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png" style="height: 65px; width: 65px;"> |
| 24 | +<pre><strong>Input:</strong> head = [1], pos = -1 |
| 25 | +<strong>Output:</strong> no cycle |
| 26 | +<strong>Explanation:</strong> There is no cycle in the linked list. |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | +<p><strong>Constraints:</strong></p> |
| 31 | + |
| 32 | +<ul> |
| 33 | + <li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li> |
| 34 | + <li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li> |
| 35 | + <li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li> |
| 36 | +</ul> |
| 37 | + |
| 38 | +<p> </p> |
| 39 | +<p><strong>Follow up:</strong> Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p> |
| 40 | +</div> |
0 commit comments