From 65a3757ad200035b098f8e46fd164426d9c4533d Mon Sep 17 00:00:00 2001 From: Cabebe Date: Sun, 22 May 2022 18:40:41 -0700 Subject: [PATCH 1/4] starts working on last methods --- linked_list/linked_list.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..b4d15268 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -52,12 +52,30 @@ def get_at_index(self, index): # Time Complexity: ? # Space Complexity: ? def get_last(self): - pass + # from coworking with vange + if not self.head: + return None + current = self.head + while current: + if not current.next: + return current.value + current = current.next + # method that inserts a given value as a new last node in the linked list # Time Complexity: ? # Space Complexity: ? def add_last(self, value): + # if not self.head: + # self.head = Node(value) + # return + + # current = self.head + # while current: + # if not current.next: + # current.next = Node(value) + # return + # current = current.next pass # method to return the max value in the linked list From d17e96d347d87398d455052e3d8ab51fd309d91b Mon Sep 17 00:00:00 2001 From: Cabebe Date: Wed, 8 Jun 2022 14:01:24 -0700 Subject: [PATCH 2/4] adds all methods up through delete --- linked_list/linked_list.py | 127 +++++++++++++++++++++++++++---------- 1 file changed, 94 insertions(+), 33 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index b4d15268..24e08a56 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -13,46 +13,78 @@ def __init__(self): # 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 self.head is None: + 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 + if self.head is None: + self.head = Node(value, self.head) + + new_node = Node(value, next_node = self.head) + self.head = new_node + + # 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(n) def search(self, value): - pass + if self.head is None: + return False + current = self.head + while current: + if current.value == value: + return True + current = current.next + return False + # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(n) def length(self): - pass + if not self.head: + return 0 + current = self.head + count = 0 + while current: + if not current.next: + return count + current = current.next + count += 1 # 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(n) def get_at_index(self, index): - pass + if self.head is None: + return None + current = self.head + current_index = 0 + while current_index < index: + if not current.next and current_index < index: + return None + current = current.next + current_index += 1 + return current.value # 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(n) def get_last(self): - # from coworking with vange if not self.head: return None current = self.head @@ -63,31 +95,60 @@ def get_last(self): # 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: def add_last(self, value): - # if not self.head: - # self.head = Node(value) - # return - - # current = self.head - # while current: - # if not current.next: - # current.next = Node(value) - # return - # current = current.next - pass + if not self.head: + self.head = Node(value, next_node = None) + current = self.head + while current: + if not current.next: + current.next = Node(value, next_node = None) + return current.next + current = current.next # method to return the max value in the linked list # returns the data value and not the node def find_max(self): - pass + if not self.head: + return None + current = self.head + max_value = self.head.value + while current: + if current.value > max_value: + max_value = current.value + current = current.next + return max_value # method to delete the first node found with specified value # Time Complexity: ? # Space Complexity: ? def delete(self, value): - pass + if not self.head: + return + if self.head.value == value: + self.head = self.head.next + return + + current = self.head + previous = None + + while current: + if current.next is None and current.value == value: + previous.next = None + return + if current.value == value: + if current.next is None: + previous.next == None + previous.next = current.next + return + previous = current + current = current.next + print(previous.value, current.value) + + + # if value isn't found, don't do anything + # method to print all the values in the linked list # Time Complexity: ? From 125cd56f3e2ece4106ee115b2d077d6f7aba17dd Mon Sep 17 00:00:00 2001 From: Cabebe Date: Mon, 25 Jul 2022 21:01:05 -0700 Subject: [PATCH 3/4] finishes delete and reverse. All tests passing --- linked_list/linked_list.py | 46 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 24e08a56..b4c34122 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -27,10 +27,10 @@ def get_first(self): # Space Complexity: O(1) def add_first(self, value): if self.head is None: - self.head = Node(value, self.head) - - new_node = Node(value, next_node = self.head) - self.head = new_node + self.head = Node(value) + else: + new_node = Node(value, next_node = self.head) + self.head = new_node @@ -56,7 +56,7 @@ def length(self): if not self.head: return 0 current = self.head - count = 0 + count = 1 while current: if not current.next: return count @@ -83,7 +83,7 @@ def get_at_index(self, index): # method that returns the value of the last node in the linked list # returns None if the linked list is empty # Time Complexity: O(n) - # Space Complexity: O(n) + # Space Complexity: O(1) def get_last(self): if not self.head: return None @@ -96,10 +96,11 @@ def get_last(self): # method that inserts a given value as a new last node in the linked list # Time Complexity: O(n) - # Space Complexity: + # Space Complexity: O(1) def add_last(self, value): if not self.head: self.head = Node(value, next_node = None) + return current = self.head while current: if not current.next: @@ -121,8 +122,8 @@ def find_max(self): return max_value # 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): if not self.head: return @@ -132,27 +133,20 @@ def delete(self, value): current = self.head previous = None - while current: - if current.next is None and current.value == value: + if not current.next and current.value == value: previous.next = None return if current.value == value: - if current.next is None: - previous.next == None previous.next = current.next return previous = current current = current.next - print(previous.value, current.value) - - - # if value isn't found, don't do anything # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(n) def visit(self): helper_list = [] current = self.head @@ -168,7 +162,19 @@ def visit(self): # Time Complexity: ? # Space Complexity: ? def reverse(self): - pass + if not self.head: + return + + previous = None + current = self.head + next = self.head.next + while next: + current.next = previous + previous = current + current = next + next = next.next + current.next = previous + self.head = current ## Advanced/ Exercises # returns the value at the middle element in the singly linked list From d365f894f2ef3a50c80ad3bffdb681438111b3f3 Mon Sep 17 00:00:00 2001 From: Cabebe Date: Mon, 25 Jul 2022 21:10:31 -0700 Subject: [PATCH 4/4] edits Big O assessments --- linked_list/linked_list.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index b4c34122..3c30cd2f 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -37,7 +37,7 @@ def add_first(self, value): # method to find if the linked list contains a node with specified value # returns true if found, false otherwise # Time Complexity: O(n) - # Space Complexity: O(n) + # Space Complexity: O(1) def search(self, value): if self.head is None: return False @@ -51,7 +51,7 @@ def search(self, value): # method that returns the length of the singly linked list # Time Complexity: O(n) - # Space Complexity: O(n) + # Space Complexity: O(1) def length(self): if not self.head: return 0 @@ -67,7 +67,7 @@ def length(self): # index count starts at 0 # returns None if there are fewer nodes in the linked list than the index value # Time Complexity: O(n) - # Space Complexity: O(n) + # Space Complexity: O(1) def get_at_index(self, index): if self.head is None: return None @@ -159,8 +159,8 @@ 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): if not self.head: return