From d47441c6a3c604e61c8ed02cba83837f24d5f854 Mon Sep 17 00:00:00 2001 From: Monica Date: Mon, 18 Apr 2022 09:53:08 -0400 Subject: [PATCH 01/13] Started exercises, firest two tests passing --- linked_list/linked_list.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..b0d62493 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -13,18 +13,22 @@ 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 + else: + return self.head # 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: ? def add_first(self, value): - pass + new_node = Node(value) + new_node.next = 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 From 1ac3b2a6283c199163219235ddd1f45f79c058f5 Mon Sep 17 00:00:00 2001 From: Monica Date: Sun, 24 Apr 2022 17:58:18 -0400 Subject: [PATCH 02/13] first 4 tests passing --- linked_list/linked_list.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index b0d62493..e5d1ae3b 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -28,14 +28,20 @@ def get_first(self): def add_first(self, value): new_node = Node(value) new_node.next = self.head - self.head = new_node - + self.head = new_node.value + # method to find if the linked list contains a node with specified value # returns true if found, false otherwise # Time Complexity: ? # Space Complexity: ? def search(self, value): - pass + current_node = self.head + while current_node is not None: + if current_node == value: + return True + current_node.next = self.next + self.next = current_node.value + return False # method that returns the length of the singly linked list # Time Complexity: ? From caaf9bf848c91643452c7ee2cc105d10075dc0ab Mon Sep 17 00:00:00 2001 From: Monica Date: Sun, 24 Apr 2022 19:59:04 -0400 Subject: [PATCH 03/13] passing search tests --- linked_list/linked_list.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index e5d1ae3b..687f4adb 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -35,13 +35,17 @@ def add_first(self, value): # Time Complexity: ? # Space Complexity: ? def search(self, value): + + # Initialize current to head current_node = self.head - while current_node is not None: + + # loop till current not equal to None + while current_node != None: if current_node == value: - return True - current_node.next = self.next - self.next = current_node.value - return False + return True # data found + current_node = current_node.next + + return False # Data Not found # method that returns the length of the singly linked list # Time Complexity: ? From 3054e46e4827d629937861678b7d0ee9037d8ecf Mon Sep 17 00:00:00 2001 From: Monica Date: Sun, 24 Apr 2022 20:27:36 -0400 Subject: [PATCH 04/13] passing length tests --- linked_list/linked_list.py | 41 ++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..8ccf1f70 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -1,4 +1,3 @@ - # Defines a node in the singly linked list class Node: @@ -13,31 +12,47 @@ 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 + else: + 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 - + new_node = Node(value) + new_node.next = 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(1) def search(self, value): - pass + current_node = self.head + print(current_node) + while current_node is not None: + if current_node.value == value: + return True + current_node = current_node.next + return False + # method that returns the length of the singly linked list # Time Complexity: ? # Space Complexity: ? def length(self): - pass + node_count = 0 + current_node = self.head + while current_node is not None: + node_count += 1 + current_node = current_node.next + return node_count # method that returns the value at a given index in the linked list # index count starts at 0 From 9f3860c6ffe324d2e077be27d1af2ca59210053a Mon Sep 17 00:00:00 2001 From: Monica Date: Tue, 26 Apr 2022 21:42:39 -0400 Subject: [PATCH 05/13] Deleted print statement --- linked_list/linked_list.py | 1 - 1 file changed, 1 deletion(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 3bd6aa97..29f74e74 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -35,7 +35,6 @@ def add_first(self, value): # Space Complexity: O(1) def search(self, value): current_node = self.head - print(current_node) while current_node is not None: if current_node.value == value: return True From 84cbeea42bf6430a155e31b2afe3d33a86f02ed6 Mon Sep 17 00:00:00 2001 From: Monica Date: Tue, 26 Apr 2022 21:56:00 -0400 Subject: [PATCH 06/13] Passing get at index tests --- linked_list/linked_list.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 29f74e74..b31daa78 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -43,8 +43,8 @@ def search(self, value): # 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): node_count = 0 current_node = self.head @@ -56,10 +56,22 @@ def length(self): # 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): + current_node = self.head + current_index = 0 + + if current_node is None: + return None + while current_node is not None: + if current_index == index: + return current_node.value + else: + current_node = current_node.next + current_index+=1 + return None # method that returns the value of the last node in the linked list # returns None if the linked list is empty From 1193bd061443b01dda3b1f0f8719df0f4e0db8fa Mon Sep 17 00:00:00 2001 From: Monica Date: Tue, 26 Apr 2022 22:19:52 -0400 Subject: [PATCH 07/13] Passing get last tests --- linked_list/linked_list.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index b31daa78..9e3011d1 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -78,7 +78,21 @@ def get_at_index(self, index): # Time Complexity: ? # Space Complexity: ? def get_last(self): - pass + current_node = self.head + + if current_node is None: + return None + + # What do I want the while loop to do? + # I want to keep the while loop going until I reach the last node + # When the last node is reached, I want to return the value + while True: + if current_node.next is None: + return current_node.value + else: + current_node = current_node.next + + # method that inserts a given value as a new last node in the linked list # Time Complexity: ? From abcd4a61eb8f2e40c5fbfe3dcb30c8df9fd8fe1c Mon Sep 17 00:00:00 2001 From: Monica Date: Thu, 28 Apr 2022 14:55:54 -0400 Subject: [PATCH 08/13] passing add last tests --- linked_list/linked_list.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 9e3011d1..6db719c9 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -75,17 +75,14 @@ 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: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_last(self): current_node = self.head if current_node is None: return None - # What do I want the while loop to do? - # I want to keep the while loop going until I reach the last node - # When the last node is reached, I want to return the value while True: if current_node.next is None: return current_node.value @@ -95,10 +92,26 @@ 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: O(1) def add_last(self, value): - pass + new_node = Node(value) + current_node = self.head + + if current_node is None: + self.head = new_node + return + + # What do I want while loop to do? I want while loop to find the last node + + # while current_node is not None: + # current_node = current_node.next + + while current_node.next: + current_node = current_node.next + + # When last node is found, I want to append new_node to it. + current_node.next = new_node # method to return the max value in the linked list # returns the data value and not the node From 0bfd2c43386820abbf7fb007189f6eb572bf32b9 Mon Sep 17 00:00:00 2001 From: Monica Date: Thu, 28 Apr 2022 15:02:09 -0400 Subject: [PATCH 09/13] passing add last tests --- linked_list/linked_list.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 6db719c9..fec81f11 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -104,11 +104,10 @@ def add_last(self, value): # What do I want while loop to do? I want while loop to find the last node - # while current_node is not None: - # current_node = current_node.next - - while current_node.next: - current_node = current_node.next + while current_node is not None: + if current_node.next is None: + break + current_node = current_node.next # When last node is found, I want to append new_node to it. current_node.next = new_node From 234a430d10eb95a6d11efdc66d8a369c39eb29f6 Mon Sep 17 00:00:00 2001 From: Monica Date: Thu, 28 Apr 2022 15:50:24 -0400 Subject: [PATCH 10/13] passing find max value tests --- linked_list/linked_list.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index fec81f11..f5708538 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -115,7 +115,21 @@ def add_last(self, value): # method to return the max value in the linked list # returns the data value and not the node def find_max(self): - pass + current_node = self.head + if current_node is None: + return None + + max_value = current_node.value + + while current_node.next: + if current_node.value > max_value: + max_value = current_node.value + current_node = current_node.next + + if current_node.value > max_value: + max_value = current_node.value + + return max_value # method to delete the first node found with specified value # Time Complexity: ? From 663348a28d73b7a46d92b3ad95145de6ce1dfac6 Mon Sep 17 00:00:00 2001 From: Monica Date: Thu, 28 Apr 2022 17:02:58 -0400 Subject: [PATCH 11/13] passing delete tests --- linked_list/linked_list.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index f5708538..3c62fd60 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -132,10 +132,32 @@ 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): - pass + current_node = self.head + previous_node = None + + if current_node is None: + return None + + # Case: value to be deleted is the first value in the linked list + if current_node.value == value: + self.head = current_node.next + current_node = None + + # Case: value to be deleted is the last value in the linked list + # traverse linked list to find the last node + # compare speficied value to last node's value + # if the values match, re-assign the penultimate node reference to None + while current_node is not None and current_node.value != value: + previous_node = current_node + current_node = current_node.next + + previous_node.next = current_node.next + current_node = None + + # method to print all the values in the linked list # Time Complexity: ? From 34aec44aee2650e3befc0d9e3b5aa2e7887748b5 Mon Sep 17 00:00:00 2001 From: Monica Date: Fri, 29 Apr 2022 16:52:20 -0400 Subject: [PATCH 12/13] passing reverse test --- linked_list/linked_list.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 3c62fd60..11c1623c 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -160,8 +160,8 @@ def delete(self, value): # 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 @@ -177,8 +177,38 @@ def visit(self): # Time Complexity: ? # Space Complexity: ? def reverse(self): - pass - + + # Put nodes in a list + # Reverse the list + # Create a new linked list from the reversed list + + current_node = self.head + nodes_list = [] + nodes_list_reverse = [] + + while current_node is not None: + nodes_list.append(current_node) + current_node = current_node.next + + i = len(nodes_list)-1 + + while i > 0: + nodes_list_reverse.append(nodes_list[i]) + i -= 1 + nodes_list_reverse.append(nodes_list[0]) + + for i in range(0, len(nodes_list_reverse)): + if i == 0: + current_node = nodes_list_reverse[i] + self.head = current_node + else: + current_node = nodes_list_reverse[i] + + if i < len(nodes_list_reverse)-1: + current_node.next = nodes_list_reverse[i+1] + + current_node.next = None + ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? From 8dde5f37a9bce709cb350739072dc64838ec66b1 Mon Sep 17 00:00:00 2001 From: Monica Date: Fri, 29 Apr 2022 17:10:27 -0400 Subject: [PATCH 13/13] Fixed delete method --- linked_list/linked_list.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 11c1623c..066740ba 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -145,6 +145,7 @@ def delete(self, value): if current_node.value == value: self.head = current_node.next current_node = None + return # Case: value to be deleted is the last value in the linked list # traverse linked list to find the last node @@ -174,8 +175,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(n) def reverse(self): # Put nodes in a list