Added notebook solving the following: Find the start of a linked list loop.

This commit is contained in:
Donne Martin 2015-05-18 14:26:50 -04:00
parent db47e1bcaa
commit e5d7fbfe0f
2 changed files with 178 additions and 0 deletions

View File

@ -27,6 +27,7 @@ Continually updated IPython Notebooks containing coding problems and solutions (
* [Delete a node in the middle of a linked list, given access to only that node](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/delete-mid.ipynb) * [Delete a node in the middle of a linked list, given access to only that node](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/delete-mid.ipynb)
* [Partition a linked list around a given value](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/partition.ipynb) * [Partition a linked list around a given value](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/partition.ipynb)
* [Add two numbers whose digits are stored in a linked list in reverse order](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/add-reverse.ipynb) * [Add two numbers whose digits are stored in a linked list in reverse order](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/add-reverse.ipynb)
* [Find the start of a linked list loop](http://nbviewer.ipython.org/github/donnemartin/algorithms-data-structures/blob/master/linked-lists/find-loop-start.ipynb)
## Stacks and Queues ## Stacks and Queues

View File

@ -0,0 +1,177 @@
{
"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
}