{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem: Implement a linked list with insert, append, find, delete, length, and print methods\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", "* 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" ] }, { "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", "### 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", "### Find\n", "\n", "* 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", "\n", "### Delete\n", "\n", "* 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", "\n", "### Length\n", "\n", "* Length of zero or more elements\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", "### 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", "### Find\n", "\n", "* 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", "\n", "Complexity:\n", "* Time: O(n)\n", "* Space: In-place\n", "\n", "### Delete\n", "\n", "* 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", "\n", "Complexity:\n", "* Time: O(n)\n", "* Space: In-place\n", "\n", "### Length\n", "\n", "* For each node\n", " * Increase length counter\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": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%writefile linked_list.py\n", "\n", "class Node(object):\n", " def __init__(self, data, next_node=None):\n", " self.next = next_node\n", " self.data = data\n", " \n", " def __str__(self):\n", " return self.data\n", "\n", "class LinkedList(object):\n", " def __init__(self, head=None):\n", " self.head = head\n", "\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", " \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 append(self, data, next_node=None):\n", " if data is None:\n", " return\n", " node = Node(data, next_node)\n", " 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", " 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", " \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", "\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": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%run linked_list.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "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": 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()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Test find\n", "# Find in an empty list\n", "linked_list = LinkedList(None)\n", "node = linked_list.find('a')\n", "print(node)\n", "# Find a NULL\n", "head = Node(10)\n", "linked_list = LinkedList(head)\n", "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)" ] }, { "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')" ] }, { "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))" ] }, { "cell_type": "code", "execution_count": null, "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 }