From 47703cde50b254a1270b06f6114492f0bd204c99 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 12 May 2015 18:57:29 -0400 Subject: [PATCH] Added append method to linked list. Saved linked list code to file for future reference in linked list problems. --- linked-lists/linked-list.ipynb | 71 +++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/linked-lists/linked-list.ipynb b/linked-lists/linked-list.ipynb index 833dfb2..9be8455 100644 --- a/linked-lists/linked-list.ipynb +++ b/linked-lists/linked-list.ipynb @@ -4,7 +4,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Implement a linked list with insert, find, delete, and print methods\n", + "## Problem: Implement a linked list with insert, append, find, delete, and print methods\n", "\n", "* [Clarifying Questions](#Clarifying-Questions)\n", "* [Test Cases](#Test-Cases)\n", @@ -41,6 +41,12 @@ "* 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", @@ -81,6 +87,20 @@ "* 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", @@ -132,6 +152,8 @@ }, "outputs": [], "source": [ + "%%writefile linked_list.py\n", + "\n", "class Node(object):\n", " def __init__(self, data):\n", " self.next = None\n", @@ -143,7 +165,7 @@ "class LinkedList(object):\n", " def __init__(self, head):\n", " self.head = head\n", - "\n", + " \n", " def insert_to_front(self, data):\n", " if data is None:\n", " return\n", @@ -154,6 +176,18 @@ " node.next = self.head\n", " self.head = node\n", " \n", + " def append(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", + " 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", @@ -188,6 +222,17 @@ " 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, @@ -210,6 +255,28 @@ "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,