{"id":149,"date":"2017-09-18T21:44:14","date_gmt":"2017-09-18T13:44:14","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=149"},"modified":"2018-11-27T16:45:10","modified_gmt":"2018-11-27T08:45:10","slug":"141-linked-list-cycle","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=149","title":{"rendered":"141. Linked List Cycle"},"content":{"rendered":"<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<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\n# Definition for singly-linked list.\r\nclass ListNode(object):\r\n    def __init__(self, x):\r\n        self.val = x\r\n        self.next = None\r\n\r\nclass Solution(object):\r\n    def hasCycle(self, head):\r\n        &quot;&quot;&quot;\r\n        :type head: ListNode\r\n        :rtype: bool\r\n        &quot;&quot;&quot;\r\n        if head is None:\r\n            return False\r\n        node1 = head\r\n        node2 = head\r\n        while True:\r\n            if node1.next is not None:\r\n                node1 = node1.next.next\r\n                node2 = node2.next\r\n                if node1 is None or node2 is None:\r\n                    return False\r\n                elif node1 == node2:\r\n                    return True\r\n            else:\r\n                return False\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a linked list, determine if it has a cycle in it.<a href=\"https:\/\/www.ccagml.com\/?p=149\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">141. Linked List Cycle<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/149"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions\/150"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}