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..df6c9ce 100644 --- a/Craking the Coding Interview/2.3_remove_node_linkedlist.py +++ b/Craking the Coding Interview/2.3_remove_node_linkedlist.py @@ -1,46 +1,35 @@ -__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 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) - 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): + # removes a value from teh linked list + 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 +38,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())