diff --git a/linked-lists/delete-mid.ipynb b/linked-lists/delete-mid.ipynb index dfd6266..f63ffd9 100644 --- a/linked-lists/delete-mid.ipynb +++ b/linked-lists/delete-mid.ipynb @@ -19,8 +19,6 @@ "source": [ "## Clarifying Questions\n", "\n", - "* Do we assume the linked list class already exists?\n", - " * No, create one\n", "* What if the final node is being deleted, for example a single node list?\n", " * Make it a dummy node" ] @@ -61,6 +59,17 @@ "## Code" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%run linked_list.py" + ] + }, { "cell_type": "code", "execution_count": null, @@ -74,10 +83,7 @@ " self.data = data\n", " self.next = None\n", "\n", - "class LinkedList(object):\n", - " def __init__(self, head):\n", - " self.head = head\n", - " \n", + "class MyLinkedList(LinkedList):\n", " def delete_node(self, node):\n", " if self.head is None:\n", " return\n", @@ -89,25 +95,7 @@ " node.next = None\n", " else:\n", " node.data = next.data\n", - " node.next = next.next\n", - "\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", - " return 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" + " node.next = next.next" ] }, { diff --git a/linked-lists/kth-to-last-elem.ipynb b/linked-lists/kth-to-last-elem.ipynb index f064e44..99b32cc 100644 --- a/linked-lists/kth-to-last-elem.ipynb +++ b/linked-lists/kth-to-last-elem.ipynb @@ -18,17 +18,13 @@ "source": [ "## Clarifying Questions\n", "\n", - "* Do we assume the linked list class already exists?\n", - " * No, create one\n", "* Is k an integer?\n", " * Yes\n", "* If k = 0, does this return the last element?\n", " * Yes\n", "* What happens if k is greater than or equal to the length of the linked list?\n", " * Return None\n", - "* Can you insert NULL values in the list?\n", - " * No\n", - "* Can use additional data structures?\n", + "* Can you use additional data structures?\n", " * No" ] }, @@ -69,6 +65,17 @@ "## Code" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%run linked_list.py" + ] + }, { "cell_type": "code", "execution_count": null, @@ -77,28 +84,12 @@ }, "outputs": [], "source": [ - "class Node(object):\n", - " def __init__(self, data):\n", - " self.data = data\n", - " self.next = None\n", - " \n", - "class LinkedList(object):\n", - " def __init__(self, head):\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", + "class MyLinkedList(LinkedList):\n", " def kth_to_last_elem(self, k):\n", " if self.head is None:\n", " return\n", " if type(k) != int:\n", - " return\n", + " raise ValueError('')\n", " if k >= len(self):\n", " return\n", " curr = self.head\n", @@ -112,20 +103,19 @@ " while curr.next is not None:\n", " curr = curr.next\n", " prev = prev.next\n", - " return prev.data\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", + " return prev.data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ "# Empty list\n", - "linked_list = LinkedList(None)\n", + "linked_list = MyLinkedList(None)\n", "print(linked_list.kth_to_last_elem(0))\n", "# k is not an integer\n", "print(linked_list.kth_to_last_elem('a'))\n", @@ -133,7 +123,7 @@ "print(linked_list.kth_to_last_elem(100))\n", "# One element, k = 0\n", "head = Node(2)\n", - "linked_list = LinkedList(head)\n", + "linked_list = MyLinkedList(head)\n", "print(linked_list.kth_to_last_elem(0))\n", "# General case with many elements, k < length of linked list\n", "linked_list.insert_to_front(1)\n", diff --git a/linked-lists/remove-duplicates.ipynb b/linked-lists/remove-duplicates.ipynb index 96e5863..cc716a5 100644 --- a/linked-lists/remove-duplicates.ipynb +++ b/linked-lists/remove-duplicates.ipynb @@ -20,13 +20,11 @@ "source": [ "## Clarifying Questions\n", "\n", - "* Do we assume the linked list class already exists?\n", - " * No, create one\n", "* Is this a singly or doubly linked list?\n", " * Singly\n", "* Can you insert NULL values in the list?\n", " * No\n", - "* Can use additional data structures?\n", + "* Can you use additional data structures?\n", " * Implement both solutions" ] }, @@ -70,6 +68,17 @@ "## Code: Hash Map Lookup" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%run linked_list.py" + ] + }, { "cell_type": "code", "execution_count": null, @@ -78,15 +87,7 @@ }, "outputs": [], "source": [ - "class Node(object):\n", - " def __init__(self, data):\n", - " self.next = None\n", - " self.data = data\n", - "\n", - "class LinkedList(object):\n", - " def __init__(self, head):\n", - " self.head = head\n", - " \n", + "class MyLinkedList(LinkedList):\n", " def remove_dupes(self):\n", " seen_data = set()\n", " curr = self.head\n", @@ -97,31 +98,24 @@ " else:\n", " seen_data.add(curr.data)\n", " prev = curr\n", - " curr = curr.next\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 = self.head\n", - " while curr is not None:\n", - " print(curr.data)\n", - " curr = curr.next\n", - " \n", + " curr = curr.next" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ "# Empty linked list\n", - "linked_list = LinkedList(None)\n", + "linked_list = MyLinkedList(None)\n", "linked_list.remove_dupes()\n", "linked_list.print_list()\n", "# One element linked list\n", "head = Node(2)\n", - "linked_list = LinkedList(head)\n", + "linked_list = MyLinkedList(head)\n", "linked_list.remove_dupes()\n", "linked_list.print_list()\n", "# Multiple elements\n", @@ -174,14 +168,7 @@ }, "outputs": [], "source": [ - "class Node(object):\n", - " def __init__(self, data):\n", - " self.next = None\n", - " self.data = data\n", - "\n", - "class LinkedList(object):\n", - " def __init__(self, head):\n", - " self.head = head\n", + "class MyLinkedList(LinkedList):\n", " \n", " def remove_dupes(self):\n", " curr = self.head\n", @@ -192,31 +179,24 @@ " runner.next = runner.next.next\n", " else:\n", " runner = runner.next\n", - " curr = curr.next\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 = self.head\n", - " while curr is not None:\n", - " print(curr.data)\n", - " curr = curr.next\n", - " \n", + " curr = curr.next" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ "# Empty linked list\n", - "linked_list = LinkedList(None)\n", + "linked_list = MyLinkedList(None)\n", "linked_list.remove_dupes()\n", "linked_list.print_list()\n", "# One element linked list\n", "head = Node(2)\n", - "linked_list = LinkedList(head)\n", + "linked_list = MyLinkedList(head)\n", "linked_list.remove_dupes()\n", "linked_list.print_list()\n", "# Multiple elements\n",