{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

141. Linked List Cycle

\n", "
\n", "\n", "\n", "

Given head, the head of a linked list, determine if the linked list has a cycle in it.

\n", "\n", "

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. Note that pos is not passed as a parameter.

\n", "\n", "

Return true if there is a cycle in the linked list. Otherwise, return false.

\n", "\n", "

 

\n", "

Example 1:

\n", "\"\"\n", "
Input: head = [3,2,0,-4], pos = 1\n",
    "Output: true\n",
    "Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).\n",
    "
\n", "

 

\n", "

Example 2:

\n", "\"\"\n", "
Input: head = [1,2], pos = 0\n",
    "Output: true\n",
    "Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.\n",
    "
\n", "

 

\n", "

Example 3:

\n", "\"\"\n", "
Input: head = [1], pos = -1\n",
    "Output: false\n",
    "Explanation: There is no cycle in the linked list.\n",
    "
\n", "\n", "

 

\n", "

Constraints:

\n", "\n", "\n", "\n", "\n", "

 

\n", "Source \n", "
\n", "\n", "

Code

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def has_cycle(head):\n", " '''Using set\n", " Time Complexity: O(n)\n", " Space Complexity O(n)\n", " '''\n", " if head is None:\n", " return False\n", " buff_set = {head}\n", " current_node = head\n", " while current_node.next:\n", " current_node = current_node.next\n", " if current_node in buff_set:\n", " return True\n", " buff_set.add(current_node)\n", " return False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Follow up:

\n", "

Can you solve it using O(1) (i.e. constant) memory?

\n", "\n", "

Code

" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def has_cycle(head):\n", " '''Using 2 pointers\n", " space complexity can be reduced to O(1) by considering 2 pointers at different speed\n", " - a slow pointer (moves 1 step at a time )\n", " - a fast pointer (moves 2 steps at a time)\n", "\n", " Cycle + case A: fast pointer is 1 step behind at time T\n", " at T+1, they meet up\n", " Cycle + Case B: fast pointer is 2 steps behind at time T\n", " at T+1 they are in case A situation\n", " at T+2 they meet up\n", "\n", " Time Complexity: O(N+K) = O(n) with:\n", " N = non-cyclic length\n", " K = cyclic length\n", " Space Complexity O(1)\n", " '''\n", " if head is None:\n", " return False\n", " slow = head\n", " fast = head.next\n", " while fast:\n", " if fast.next is None:\n", " return False\n", " fast = fast.next.next\n", " slow = slow.next\n", " if slow == fast:\n", " return True\n", " return False" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }