From 72a7fc48d0643b298e427e0fb56db862c86d0f7a Mon Sep 17 00:00:00 2001 From: Asli Athman Date: Thu, 21 Jul 2022 18:46:07 -0400 Subject: [PATCH] Answers all exercies, passes all tests --- linked_list/linked_list.py | 163 ++++++++++++++++++++++++++++--------- tests/linked_list_test.py | 7 +- 2 files changed, 125 insertions(+), 45 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..9e6d1476 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -9,67 +9,124 @@ def __init__(self, value, next_node = None): # Defines the singly linked list class LinkedList: def __init__(self): - self.head = None # keep the head private. Not accessible outside this class + self.head = None # keep the head private. Not accessible outside this class # returns the value in the first node # returns None if the list is empty - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(1) + # Space Complexity: O(1) def get_first(self): - pass + if not self.head: + return None + + return self.head.value # method to add a new node with the specific data value in the linked list # insert the new node at the beginning of the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(1) + # Space Complexity: O(1) def add_first(self, value): - pass + added = Node(value) + added.next = self.head + self.head = added # method to find if the linked list contains a node with specified value # returns true if found, false otherwise - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def search(self, value): - pass + curr = self.head + while curr: + if curr.value == value: + return True + curr = curr.next + return False # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def length(self): - pass + len = 0 + curr = self.head + while curr: + len += 1 + curr = curr.next + return len + # method that returns the value at a given index in the linked list # index count starts at 0 # returns None if there are fewer nodes in the linked list than the index value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_at_index(self, index): - pass + i = 0 + curr = self.head + while curr: + if i == index: + return curr.value + i += 1 + curr = curr.next + + return None # method that returns the value of the last node in the linked list # returns None if the linked list is empty - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_last(self): - pass + if not self.head: + return None + curr = self.head + while curr.next: + curr = curr.next + return curr.value # method that inserts a given value as a new last node in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def add_last(self, value): - pass + if not self.head: + self.head = Node(value) + return + curr = self.head + while curr.next: + curr = curr.next + curr.next = Node(value) # method to return the max value in the linked list # returns the data value and not the node + #Time Complexity: O(n) + #Space Complexity: O(1) def find_max(self): - pass + max = 0 + if not self.head: + return None + curr = self.head + while curr: + if curr.value > max: + max = curr.value + curr = curr.next + return max # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def delete(self, value): - pass + curr = self.head + prev = None + if not curr: + return None + if curr.value == value: + self.head = curr.next + return + while curr: + if curr.value == value: + prev.next = curr.next + return + prev = curr + curr = curr.next # method to print all the values in the linked list # Time Complexity: ? @@ -86,32 +143,60 @@ def visit(self): # method to reverse the singly linked list # note: the nodes should be moved and not just the values in the nodes - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def reverse(self): - pass - + if not self.head or not self.head.next: + return + if self.length() == 2: + second = self.head.next + self.head.next = None + second.next = self.head + self.head = second + return + + prev = self.head + curr = self.head.next + next = curr.next + while next: + curr.next = prev + prev = curr + curr = next + next = next.next + curr.next = prev + self.head.next = None + self.head = curr + ## Advanced/ Exercises # returns the value at the middle element in the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def find_middle_value(self): - pass + mid = self.length() // 2 + return self.get_at_index(mid) # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def find_nth_from_end(self, n): - pass + len = self.length() + return self.get_at_index(len - 1 - n) # checks if the linked list has a cycle. A cycle exists if any node in the # linked list links to a node already visited. # returns true if a cycle is found, false otherwise. - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(n) def has_cycle(self): - pass + node_map = {} + curr = self.head + while curr: + if node_map.get(curr): + return True + node_map[curr] = True + curr = curr.next + return False # Helper method for tests # Creates a cycle in the linked list for testing purposes diff --git a/tests/linked_list_test.py b/tests/linked_list_test.py index 58a9d0cf..6fbd9abd 100644 --- a/tests/linked_list_test.py +++ b/tests/linked_list_test.py @@ -197,7 +197,6 @@ def test_reverse_will_reverse_five_element_list(list): for i in range(0, 5): assert list.get_at_index(i) == i -@pytest.mark.skip(reason="Going Further methods") def test_find_middle_value_returns_middle_element_of_five_element_list(list): list.add_first(10) list.add_first(30) @@ -206,7 +205,6 @@ def test_find_middle_value_returns_middle_element_of_five_element_list(list): list.add_first(20) assert list.find_middle_value() == 50 -@pytest.mark.skip(reason="Going Further methods") def test_find_middle_value_returns_element_at_index_two_of_six_element_list(list): list.add_first(10) list.add_first(30) @@ -216,11 +214,9 @@ def test_find_middle_value_returns_element_at_index_two_of_six_element_list(list list.add_first(100) assert list.find_middle_value() == 60 -@pytest.mark.skip(reason="Going Further methods") def test_nth_from_n_when_list_is_empty(list): assert list.find_nth_from_end(3) == None -@pytest.mark.skip(reason="Going Further methods") def test_find_nth_from_n_when_length_less_than_n(list): list.add_first(5) list.add_first(4) @@ -230,7 +226,6 @@ def test_find_nth_from_n_when_length_less_than_n(list): assert list.find_nth_from_end(6) == None -@pytest.mark.skip(reason="Going Further methods") def test_find_nth_from_n(list): list.add_first(1) list.add_first(2) @@ -243,7 +238,7 @@ def test_find_nth_from_n(list): assert list.find_nth_from_end(3) == 4 assert list.find_nth_from_end(4) == None -@pytest.mark.skip(reason="Going Further methods") + def test_has_cycle(list): assert list.has_cycle() == False