From 07d297790700698d34cfe1c7c313af4d4f6239dc Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Sat, 8 Jul 2017 09:26:10 -0500 Subject: [PATCH 1/2] Update 2.3_remove_node_linkedlist.py i think there is no need for two remove functions we can do this with one remove function --- .../2.3_remove_node_linkedlist.py | 42 ++++++------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/Craking the Coding Interview/2.3_remove_node_linkedlist.py b/Craking the Coding Interview/2.3_remove_node_linkedlist.py index 65d7f01..659c79c 100644 --- a/Craking the Coding Interview/2.3_remove_node_linkedlist.py +++ b/Craking the Coding Interview/2.3_remove_node_linkedlist.py @@ -1,14 +1,13 @@ -__author__ = 'Minsuk Heo' +__author__ = 'Minsuk Heo & Chris Jones' """ Linked List """ -import unittest - class Node: def __init__(self, item): self.val = item self.next = None + class LinkedList: def __init__(self, item): self.head = Node(item) @@ -19,28 +18,16 @@ def add(self, item): cur = cur.next cur.next = Node(item) - def remove(self, item): - if self.head.val == item: - self.head = self.head.next - else: - cur = self.head - while cur.next is not None: - if cur.val == item: - self.removeItem(item) - return - cur = cur.next - print("item does not exist in linked list") - - def removeItem(self, item): + def remove_item(self, item): cur = self.head while cur.next is not None: if cur.next.val == item: - nextnode = cur.next.next - cur.next = nextnode + next_node = cur.next.next + cur.next = next_node break cur = cur.next - def printlist(self): + def print_list(self): cur = self.head res = [] while cur is not None: @@ -49,13 +36,10 @@ def printlist(self): return str(res) -class LinkedListTest(unittest.TestCase): - def test(self): - ll = LinkedList(9) - ll.add(5) - ll.add(8) - ll.add(7) - ll.add(6) - ll.remove(8) - self.assertEqual("[9, 5, 7, 6]", ll.printlist()) - +ll = LinkedList(9) +ll.add(5) +ll.add(8) +ll.add(7) +ll.add(6) +ll.remove_item(8) +print(ll.print_list()) From def7ccb1fcee34dd8b5fd4874e1e5b19dbeecb93 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Sat, 8 Jul 2017 09:28:15 -0500 Subject: [PATCH 2/2] Update 2.3_remove_node_linkedlist.py simple comments added --- Craking the Coding Interview/2.3_remove_node_linkedlist.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Craking the Coding Interview/2.3_remove_node_linkedlist.py b/Craking the Coding Interview/2.3_remove_node_linkedlist.py index 659c79c..df6c9ce 100644 --- a/Craking the Coding Interview/2.3_remove_node_linkedlist.py +++ b/Craking the Coding Interview/2.3_remove_node_linkedlist.py @@ -7,17 +7,19 @@ def __init__(self, item): self.val = item self.next = None - +# Class to handle the linked list options class LinkedList: def __init__(self, item): self.head = Node(item) + # Adds a value to the linked list def add(self, item): cur = self.head while cur.next is not None: cur = cur.next cur.next = Node(item) + # removes a value from teh linked list def remove_item(self, item): cur = self.head while cur.next is not None: