2015-05-06 05:42:34 +08:00
{
"cells": [
2015-06-18 04:39:18 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
]
},
2015-05-06 05:42:34 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-05-14 19:34:35 +08:00
"## Problem: Implement a linked list with insert, append, find, delete, length, and print methods\n",
2015-05-06 05:42:34 +08:00
"\n",
"* [Clarifying Questions](#Clarifying-Questions)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
2015-05-14 19:30:13 +08:00
"* [Code](#Code)"
2015-05-06 05:42:34 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clarifying Questions\n",
"\n",
"* Is this a singly or doubly linked list?\n",
" * Singly\n",
"* Is this a circular list?\n",
" * No\n",
"* Do we keep track of the tail or just the head?\n",
2015-05-15 18:12:38 +08:00
" * Just the head"
2015-05-06 05:42:34 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"### Insert to Front\n",
"\n",
"* Insert a NULL\n",
"* Insert in an empty list\n",
"* Insert in a list with one element or more elements\n",
"\n",
2015-05-13 06:57:29 +08:00
"### Append\n",
"\n",
"* Append a NULL\n",
"* Append in an empty list\n",
"* Insert in a list with one element or more elements\n",
"\n",
2015-05-06 05:42:34 +08:00
"### Find\n",
"\n",
2015-05-06 05:47:37 +08:00
"* Find a NULL\n",
"* Find in an empty list\n",
"* Find in a list with one element or more matching elements\n",
"* Find in a list with no matches\n",
2015-05-06 05:42:34 +08:00
"\n",
"### Delete\n",
"\n",
2015-05-09 04:49:33 +08:00
"* Delete a NULL\n",
"* Delete in an empty list\n",
"* Delete in a list with one element or more matching elements\n",
"* Delete in a list with no matches\n",
2015-05-06 05:42:34 +08:00
"\n",
2015-05-14 19:34:35 +08:00
"### Length\n",
"\n",
"* Length of zero or more elements\n",
"\n",
2015-05-06 05:42:34 +08:00
"### Print\n",
"\n",
"* Print an empty list\n",
"* Print a list with one or more elements"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"### Insert to Front\n",
"\n",
"* If the data we are inserting is NULL, return\n",
"* Create a node with the input data\n",
"* If this is an empty list\n",
" * Assign the head to the node\n",
"* Else\n",
" * Set the node.next to the head\n",
" * Set the head to the node\n",
"\n",
"Complexity:\n",
"* Time: O(1)\n",
"* Space: O(1), a new node is added\n",
"\n",
2015-05-13 06:57:29 +08:00
"### Append\n",
"\n",
"* If the data we are inserting is NULL, return\n",
"* Create a node with the input data\n",
"* If this is an empty list\n",
" * Assign the head to the node\n",
"* Else\n",
" * Iterate to the end of the list\n",
" * Set the final node's next to the new node\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(1), a new node is added\n",
"\n",
2015-05-06 05:42:34 +08:00
"### Find\n",
"\n",
2015-05-06 05:47:37 +08:00
"* If data we are finding is NULL, return\n",
"* If the list is empty, return\n",
"* For each node\n",
" * If the value is a match, return it\n",
" * Else, move on to the next node\n",
2015-05-06 05:42:34 +08:00
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: In-place\n",
"\n",
"### Delete\n",
"\n",
2015-05-09 04:49:33 +08:00
"* If data we are deleting is NULL, return\n",
"* If the list is empty, return\n",
"* For each node, keep track of previous and current node\n",
" * If the value we are deleting is a match in the current node\n",
" * Update previous node's next pointer to the current node's next pointer\n",
" * We do not have have to explicitly delete in Python\n",
" * Else, move on to the next node\n",
2015-05-06 05:42:34 +08:00
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: In-place\n",
"\n",
2015-05-14 19:34:35 +08:00
"### Length\n",
"\n",
"* For each node\n",
" * Increase length counter\n",
" \n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: In-place\n",
"\n",
2015-05-06 05:42:34 +08:00
"### Print\n",
"\n",
"* For each node\n",
" * Print the node's value\n",
" \n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: In-place"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
2015-05-06 05:47:37 +08:00
"execution_count": null,
2015-05-06 05:42:34 +08:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
2015-05-13 06:57:29 +08:00
"%%writefile linked_list.py\n",
"\n",
2015-05-06 05:42:34 +08:00
"class Node(object):\n",
2015-05-14 19:30:13 +08:00
" def __init__(self, data, next_node=None):\n",
" self.next = next_node\n",
2015-05-06 05:42:34 +08:00
" self.data = data\n",
" \n",
" def __str__(self):\n",
" return self.data\n",
"\n",
"class LinkedList(object):\n",
2015-05-14 19:30:13 +08:00
" def __init__(self, head=None):\n",
2015-05-06 05:42:34 +08:00
" self.head = head\n",
2015-05-14 19:34:35 +08:00
"\n",
" def __len__(self):\n",
" curr = self.head\n",
" counter = 0\n",
" while curr is not None:\n",
" counter += 1\n",
" curr = curr.next\n",
" return counter\n",
2015-05-13 06:57:29 +08:00
" \n",
2015-05-06 05:42:34 +08:00
" def insert_to_front(self, data):\n",
" if data is None:\n",
" return\n",
" node = Node(data)\n",
" if self.head is None:\n",
" self.head = node\n",
" else:\n",
" node.next = self.head\n",
" self.head = node\n",
2015-05-06 05:47:37 +08:00
" \n",
2015-05-14 19:30:13 +08:00
" def append(self, data, next_node=None):\n",
2015-05-13 06:57:29 +08:00
" if data is None:\n",
" return\n",
2015-05-14 19:30:13 +08:00
" node = Node(data, next_node)\n",
2015-05-13 06:57:29 +08:00
" if self.head is None:\n",
" self.head = node\n",
" else:\n",
" curr_node = self.head\n",
" while curr_node.next is not None:\n",
" curr_node = curr_node.next\n",
" curr_node.next = node\n",
" \n",
2015-05-06 05:47:37 +08:00
" def find(self, data):\n",
" if data is None:\n",
" return\n",
" if self.head is None:\n",
" return\n",
" curr_node = self.head\n",
" while curr_node is not None:\n",
" if curr_node.data == data:\n",
" return curr_node\n",
" else:\n",
" curr_node = curr_node.next\n",
2015-05-09 04:49:33 +08:00
" \n",
" def delete(self, data):\n",
" if data is None:\n",
" return\n",
" if self.head is None:\n",
" return\n",
" prev_node = self.head\n",
" curr_node = prev_node.next\n",
" while curr_node is not None:\n",
" if curr_node.data == data:\n",
" prev_node.next = curr_node.next\n",
" return\n",
" else:\n",
" prev_node = curr_node\n",
" curr_node = curr_node.next\n",
2015-05-06 05:42:34 +08:00
"\n",
" def print_list(self):\n",
" curr_node = self.head\n",
" while curr_node is not None:\n",
" print(curr_node.data)\n",
" curr_node = curr_node.next"
]
},
2015-05-13 06:57:29 +08:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run linked_list.py"
]
},
2015-05-06 05:42:34 +08:00
{
"cell_type": "code",
2015-05-06 05:47:37 +08:00
"execution_count": null,
2015-05-06 05:42:34 +08:00
"metadata": {
"collapsed": false
},
2015-05-06 05:47:37 +08:00
"outputs": [],
2015-05-06 05:42:34 +08:00
"source": [
"# Test insert_to_front\n",
"# Insert in an empty list\n",
"linked_list = LinkedList(None)\n",
"linked_list.insert_to_front(10)\n",
"linked_list.print_list()\n",
"# Insert a NULL\n",
"linked_list.insert_to_front(None)\n",
"linked_list.print_list()\n",
"# Insert in a list with one element or more elements\n",
"linked_list.insert_to_front('a')\n",
"linked_list.insert_to_front('bc')\n",
"linked_list.print_list()"
]
},
2015-05-13 06:57:29 +08:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Test append\n",
"# Insert in an empty list\n",
"linked_list = LinkedList(None)\n",
"linked_list.append(10)\n",
"linked_list.print_list()\n",
"# Insert a NULL\n",
"linked_list.append(None)\n",
"linked_list.print_list()\n",
"# Insert in a list with one element or more elements\n",
"linked_list.append('a')\n",
"linked_list.append('bc')\n",
"linked_list.print_list()"
]
},
2015-05-06 05:42:34 +08:00
{
"cell_type": "code",
2015-05-06 05:47:37 +08:00
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Test find\n",
2015-05-09 04:49:33 +08:00
"# Find in an empty list\n",
2015-05-06 05:47:37 +08:00
"linked_list = LinkedList(None)\n",
"node = linked_list.find('a')\n",
"print(node)\n",
2015-05-09 04:49:33 +08:00
"# Find a NULL\n",
2015-05-06 05:47:37 +08:00
"head = Node(10)\n",
2015-05-09 04:49:33 +08:00
"linked_list = LinkedList(head)\n",
2015-05-06 05:47:37 +08:00
"node = linked_list.find(None)\n",
"print(node)\n",
"# Find in a list with one element or more matching elements\n",
"head = Node(10)\n",
"linked_list = LinkedList(head)\n",
"linked_list.insert_to_front('a')\n",
"linked_list.insert_to_front('bc')\n",
"node = linked_list.find('a')\n",
"print(node)\n",
"# Find in a list with no matches\n",
"node = linked_list.find('aaa')\n",
"print(node)"
]
},
2015-05-09 04:49:33 +08:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Test delete\n",
"# Delete in an empty list\n",
"linked_list = LinkedList(None)\n",
"linked_list.delete('a')\n",
"linked_list.print_list()\n",
"# Delete a NULL\n",
"head = Node(10)\n",
"linked_list = LinkedList(head)\n",
"linked_list.delete(None)\n",
"linked_list.print_list()\n",
"# Delete in a list with one element or more matching elements\n",
"head = Node(10)\n",
"linked_list = LinkedList(head)\n",
"linked_list.insert_to_front('a')\n",
"linked_list.insert_to_front('bc')\n",
"linked_list.delete('a')\n",
"linked_list.print_list()\n",
"# Delete in a list with no matches\n",
"linked_list.delete('aa')"
]
},
2015-05-14 19:34:35 +08:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Test length\n",
"# Zero elements\n",
"linked_list = LinkedList(None)\n",
"print(len(linked_list))\n",
"# One or more elements\n",
"head = Node(10)\n",
"linked_list = LinkedList(head)\n",
"linked_list.insert_to_front('a')\n",
"linked_list.insert_to_front('bc')\n",
"print(len(linked_list))"
]
},
2015-05-06 05:47:37 +08:00
{
"cell_type": "code",
"execution_count": null,
2015-05-06 05:42:34 +08:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Test empty print\n",
"linked_list = LinkedList(None)\n",
"linked_list.print_list()"
]
}
],
"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",
2015-06-18 04:39:18 +08:00
"version": "2.7.10"
2015-05-06 05:42:34 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 0
}