{"id":8862,"date":"2021-11-28T13:23:00","date_gmt":"2021-11-28T21:23:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8862"},"modified":"2021-11-28T13:28:45","modified_gmt":"2021-11-28T21:28:45","slug":"leetcode-142-linked-list-cycle-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-142-linked-list-cycle-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 142. Linked List Cycle II"},"content":{"rendered":"\n<p>Given the&nbsp;<code>head<\/code>&nbsp;of a linked list, return&nbsp;<em>the node where the cycle begins. If there is no cycle, return&nbsp;<\/em><code>null<\/code>.<\/p>\n\n\n\n<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&nbsp;<code>next<\/code>&nbsp;pointer. Internally,&nbsp;<code>pos<\/code>&nbsp;is used to denote the index of the node that tail&#8217;s&nbsp;<code>next<\/code>&nbsp;pointer is connected to (<strong>0-indexed<\/strong>). It is&nbsp;<code>-1<\/code>&nbsp;if there is no cycle.&nbsp;<strong>Note that<\/strong>&nbsp;<code>pos<\/code>&nbsp;<strong>is not passed as a parameter<\/strong>.<\/p>\n\n\n\n<p><strong>Do not modify<\/strong>&nbsp;the linked list.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/07\/circularlinkedlist.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [3,2,0,-4], pos = 1\n<strong>Output:<\/strong> tail connects to node index 1\n<strong>Explanation:<\/strong> There is a cycle in the linked list, where tail connects to the second node.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/07\/circularlinkedlist_test2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,2], pos = 0\n<strong>Output:<\/strong> tail connects to node index 0\n<strong>Explanation:<\/strong> There is a cycle in the linked list, where tail connects to the first node.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2018\/12\/07\/circularlinkedlist_test3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1], pos = -1\n<strong>Output:<\/strong> no cycle\n<strong>Explanation:<\/strong> There is no cycle in the linked list.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of the nodes in the list is in the range&nbsp;<code>[0, 10<sup>4<\/sup>]<\/code>.<\/li><li><code>-10<sup>5<\/sup>&nbsp;&lt;= Node.val &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>pos<\/code>&nbsp;is&nbsp;<code>-1<\/code>&nbsp;or a&nbsp;<strong>valid index<\/strong>&nbsp;in the linked-list.<\/li><\/ul>\n\n\n\n<p><strong>Follow up:<\/strong>&nbsp;Can you solve it using&nbsp;<code>O(1)<\/code>&nbsp;(i.e. constant) memory?<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Hashtset<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode *detectCycle(ListNode *head) {\n    unordered_set<ListNode*> s;\n    for (ListNode* cur = head; cur; cur = cur->next)\n      if (!s.insert(cur).second) return cur;\n    return nullptr;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Fast slow pointers<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  ListNode *detectCycle(ListNode *head) {\n    unordered_set<ListNode*> s;\n    for (ListNode* cur = head; cur; cur = cur->next)\n      if (!s.insert(cur).second) return cur;\n    return nullptr;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;head&nbsp;of a linked list, return&nbsp;the node where the cycle begins. If there is no cycle, return&nbsp;null. There is a cycle in a linked list&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[123,82,83,177],"class_list":["post-8862","post","type-post","status-publish","format-standard","hentry","category-list","tag-cycle","tag-hashtable","tag-list","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8862"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8862\/revisions"}],"predecessor-version":[{"id":8865,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8862\/revisions\/8865"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}