mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
218 lines
4.7 KiB
Plaintext
218 lines
4.7 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Problem: Implement a linked list with insert, find, delete, and print methods.\n",
|
||
|
"\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",
|
||
|
"* Coming soon\n",
|
||
|
"\n",
|
||
|
"### Delete\n",
|
||
|
"\n",
|
||
|
"* Coming soon\n",
|
||
|
"\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",
|
||
|
"* Coming soon\n",
|
||
|
"\n",
|
||
|
"Complexity:\n",
|
||
|
"* Time: O(n)\n",
|
||
|
"* Space: In-place\n",
|
||
|
"\n",
|
||
|
"### Delete\n",
|
||
|
"\n",
|
||
|
"* Coming soon\n",
|
||
|
"\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",
|
||
|
"execution_count": 19,
|
||
|
"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",
|
||
|
"\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",
|
||
|
"execution_count": 30,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"10\n",
|
||
|
"10\n",
|
||
|
"bc\n",
|
||
|
"a\n",
|
||
|
"10\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"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",
|
||
|
"execution_count": 31,
|
||
|
"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
|
||
|
}
|