{"id":3683,"date":"2018-08-24T09:18:07","date_gmt":"2018-08-24T16:18:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=3683"},"modified":"2018-08-24T09:18:20","modified_gmt":"2018-08-24T16:18:20","slug":"leetcode-141-linked-list-cycle","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-141-linked-list-cycle\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 141. Linked List Cycle"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 141. Linked List Cycle - \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5c EP20\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/bxCb37nLXWM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<div class=\"question-description__3U1T\">\n<div>\n<p>Given a linked list, determine if it has a cycle in it.<\/p>\n<p>Follow up:<br \/>\nCan you solve it without using extra space?<\/p>\n<h1><strong>Solution1: HashTable<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\npublic:\r\n  bool hasCycle(ListNode *head) {\r\n    unordered_set&lt;ListNode*&gt; seen;\r\n    while (head) {\r\n      if (seen.count(head)) return true;\r\n      seen.insert(head);\r\n      head = head-&gt;next;\r\n    }\r\n    return false;\r\n  }\r\n};<\/pre>\n<\/div>\n<\/div>\n<h1><strong>Solution2: Fast + Slow pointers<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 8 ms\r\nclass Solution {\r\npublic:\r\n  bool hasCycle(ListNode *head) {\r\n    auto slow = head;\r\n    auto fast = head;\r\n    while (fast) {\r\n      if (!fast-&gt;next) return false;\r\n      fast = fast-&gt;next-&gt;next;\r\n      slow = slow-&gt;next;\r\n      if (fast == slow) return true;\r\n    }\r\n    return false;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Solution1: HashTable&#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,352,83],"class_list":["post-3683","post","type-post","status-publish","format-standard","hentry","category-list","tag-cycle","tag-fast-slow","tag-list","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3683","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=3683"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3683\/revisions"}],"predecessor-version":[{"id":3686,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3683\/revisions\/3686"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}