interactive-coding-challenges/linked-lists/find-loop-start.ipynb

178 lines
4.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Find the start of a linked list loop.\n",
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clarifying Questions\n",
"\n",
"* This is a singly linked list?\n",
" * Yes\n",
"* Can we assume we are always passed a circular linked list?\n",
" * No"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* Empty list\n",
"* Not a circular linked list\n",
" * One element\n",
" * Two elements\n",
" * Three or more elements\n",
"* General case"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"* Check for an empty list\n",
"* Use two pointers i, j, initialized to the head\n",
"* j is incremented twice as fast as i\n",
" * If j's next is NULL, we do not have a circular list\n",
"* When i and j meet, move j to the head\n",
"* Increment i and j one node at a time until they meet\n",
"* Where they meet is the start of the loop\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: In-place"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run linked_list.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class MyLinkedList(LinkedList):\n",
" def find_loop_start(self):\n",
" if self.head is None or self.head.next is None:\n",
" return\n",
" i = self.head\n",
" j = self.head\n",
" i = i.next\n",
" j = j.next.next\n",
" while j != i:\n",
" i = i.next\n",
" if j is None or j.next is None:\n",
" return\n",
" j = j.next.next\n",
" j = self.head\n",
" while j != i:\n",
" i = i.next\n",
" j = j.next\n",
" return i.data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print('Empty list')\n",
"linked_list = MyLinkedList()\n",
"print(linked_list.find_loop_start())\n",
"print('Not a circular linked list')\n",
"print('One element')\n",
"head = Node(1)\n",
"linked_list = MyLinkedList(head)\n",
"print(linked_list.find_loop_start())\n",
"print('Two elements')\n",
"linked_list.append(2)\n",
"print(linked_list.find_loop_start())\n",
"print('Three or more elements')\n",
"linked_list.append(3)\n",
"print(linked_list.find_loop_start())\n",
"print('General case')\n",
"node0 = Node(0)\n",
"node1 = Node(1)\n",
"node2 = Node(2)\n",
"node3 = Node(3)\n",
"node4 = Node(4)\n",
"node5 = Node(5)\n",
"node6 = Node(6)\n",
"node7 = Node(7)\n",
"node8 = Node(8)\n",
"node9 = Node(9)\n",
"node10 = Node(10)\n",
"node0.next = node1\n",
"node1.next = node2\n",
"node2.next = node3\n",
"node3.next = node4\n",
"node4.next = node5\n",
"node5.next = node6\n",
"node6.next = node7\n",
"node7.next = node8\n",
"node8.next = node9\n",
"node9.next = node10\n",
"node10.next = node3\n",
"linked_list = MyLinkedList(node0)\n",
"print(linked_list.find_loop_start())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}