{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>141. Linked List Cycle</h1>\n",
    "<hr>\n",
    "\n",
    "<!--Copy Paste Leetcode statement between-->\n",
    "<p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>\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, <code>pos</code>&nbsp;is used to denote the index of the node that&nbsp;tail's&nbsp;<code>next</code>&nbsp;pointer is connected to.&nbsp;<strong>Note that&nbsp;<code>pos</code>&nbsp;is not passed as a parameter</strong>.</p>\n",
    "\n",
    "<p>Return&nbsp;<code>true</code><em> if there is a cycle in the linked list</em>. Otherwise, return <code>false</code>.</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "<p><strong>Example 1:</strong></p>\n",
    "<img alt=\"\" src=\"./img1.png\" style=\"width: 300px; height: 97px; margin-top: 8px; margin-bottom: 8px;\">\n",
    "<pre><strong>Input:</strong> head = [3,2,0,-4], pos = 1\n",
    "<strong>Output:</strong> true\n",
    "<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).\n",
    "</pre>\n",
    "<p>&nbsp;</p>\n",
    "<p><strong>Example 2:</strong></p>\n",
    "<img alt=\"\" src=\"./img2.png\" style=\"width: 141px; height: 74px;\">\n",
    "<pre><strong>Input:</strong> head = [1,2], pos = 0\n",
    "<strong>Output:</strong> true\n",
    "<strong>Explanation:</strong> There is a cycle in the linked list, where the tail connects to the 0th node.\n",
    "</pre>\n",
    "<p>&nbsp;</p>\n",
    "<p><strong>Example 3:</strong></p>\n",
    "<img alt=\"\" src=\"./img3.png\" style=\"width: 45px; height: 45px;\">\n",
    "<pre><strong>Input:</strong> head = [1], pos = -1\n",
    "<strong>Output:</strong> false\n",
    "<strong>Explanation:</strong> There is no cycle in the linked list.\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "<p><strong>Constraints:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.</li>\n",
    "\t<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>pos</code> is <code>-1</code> or a <strong>valid index</strong> in the linked-list.</li>\n",
    "</ul>\n",
    "<!--Copy Paste Leetcode statement between-->\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "<a href=\"https://leetcode.com/problems/linked-list-cycle/\">Source</a> \n",
    "<hr>\n",
    "\n",
    "<h4>Code</h4>"
   ]
  },
  {
   "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": [
    "<hr>\n",
    "<h4>Follow up:</h4>\n",
    "<p>Can you solve it using <code>O(1)</code> (i.e. constant) memory?</p>\n",
    "\n",
    "<h4>Code</h4>"
   ]
  },
  {
   "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
}