2015-05-06 05:42:34 +08:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
2015-05-09 04:49:33 +08:00
|
|
|
"## Problem: Implement a linked list with insert, find, delete, 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",
|
|
|
|
"* [Code](#Code)\n",
|
|
|
|
"* [Pythonic-Code](#Pythonic-Code)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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",
|
|
|
|
" * Just the head\n",
|
|
|
|
"* Can you insert NULL values in the list?\n",
|
|
|
|
" * No"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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",
|
|
|
|
"### 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",
|
|
|
|
"### 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",
|
|
|
|
"### 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",
|
|
|
|
"### 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": [
|
|
|
|
"class Node(object):\n",
|
|
|
|
" def __init__(self, data):\n",
|
|
|
|
" self.next = None\n",
|
|
|
|
" self.data = data\n",
|
|
|
|
" \n",
|
|
|
|
" def __str__(self):\n",
|
|
|
|
" return self.data\n",
|
|
|
|
"\n",
|
|
|
|
"class LinkedList(object):\n",
|
|
|
|
" def __init__(self, head):\n",
|
|
|
|
" self.head = head\n",
|
|
|
|
"\n",
|
|
|
|
" 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",
|
|
|
|
" 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"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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()"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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-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",
|
|
|
|
"version": "2.7.9"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"nbformat": 4,
|
|
|
|
"nbformat_minor": 0
|
|
|
|
}
|