From e914d0cc2d481422d09c7122ad7efaf1e95fb97f Mon Sep 17 00:00:00 2001 From: Rebeca Date: Tue, 28 Jun 2022 21:57:58 -0700 Subject: [PATCH 1/2] passing 20 tests --- linked_list/__init__.py | 4 ++ linked_list/linked_list.py | 97 +++++++++++++++++++++++++++++++------- 2 files changed, 83 insertions(+), 18 deletions(-) diff --git a/linked_list/__init__.py b/linked_list/__init__.py index e69de29b..bc0cbb34 100644 --- a/linked_list/__init__.py +++ b/linked_list/__init__.py @@ -0,0 +1,4 @@ +class Node: + def __init__(self, value): + self.data = value + self.next = None \ No newline at end of file diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..ba7817ad 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -1,7 +1,9 @@ # Defines a node in the singly linked list -class Node: +from platform import node +#from sys import last_value +class Node: def __init__(self, value, next_node = None): self.value = value self.next = next_node @@ -9,35 +11,47 @@ 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: ? + # Time Complexity: ? O(n) # Space Complexity: ? + def get_first(self): - pass - + if self.head == 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: ? def add_first(self, value): - pass + self.head = Node(value, self.head) # 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 = self.head + while current != None: + 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: ? def length(self): - pass + current = self.head + count = 0 + while current: + count += 1 + current = current.next + return count # method that returns the value at a given index in the linked list # index count starts at 0 @@ -45,31 +59,71 @@ def length(self): # Time Complexity: ? # Space Complexity: ? def get_at_index(self, index): - pass + current = self.head #initialise temp + count = 0 #index of current node + while (current): + if (count == index): + return current.value + count +=1 + current = current.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: ? def get_last(self): - pass + current = self.head + if self.head == None: + return None + while current.next != None: + current = current.next + return current.value # method that inserts a given value as a new last node in the linked list - # Time Complexity: ? + # Time Complexity: O(n) # Space Complexity: ? def add_last(self, value): - pass - + new_node = Node(value) + if self.head == None: + self.head = new_node + return + last = self.head + while (last.next): + last = last.next + last.next = new_node + # method to return the max value in the linked list # returns the data value and not the node def find_max(self): - pass + head = self.head + if (self.head == None): + return None + else: + max = head.value + + while (head != None): + if (max < head.value): + max = head.value + head = head.next + #print("Maximum value node in the list: " + str(max)) # method to delete the first node found with specified value # Time Complexity: ? # Space Complexity: ? - def delete(self, value): - pass + def delete(self, target_node_data): + if self.head is None: + raise Exception("List is Empty") + if self.head.valuecl == target_node_data: + self.head = self.head.next + + previous_node = self.head + for node in self: + if node.data == target_node_data: + previous_node.next = node.next + return + previous_node = node + return None # method to print all the values in the linked list # Time Complexity: ? @@ -89,7 +143,14 @@ def visit(self): # Time Complexity: ? # Space Complexity: ? def reverse(self): - pass + prev = None + current = self.head + while current is not None: + next = current.next + current.next = prev + prev = current + current = next + self.head = prev ## Advanced/ Exercises # returns the value at the middle element in the singly linked list From c2a43e862234c9110a9e9c86e19f8571708cf2a1 Mon Sep 17 00:00:00 2001 From: Rebeca Date: Wed, 29 Jun 2022 14:47:02 -0700 Subject: [PATCH 2/2] all tests passing --- linked_list/linked_list.py | 61 +++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index ba7817ad..857fad03 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -15,8 +15,8 @@ def __init__(self): # returns the value in the first node # returns None if the list is empty - # Time Complexity: ? O(n) - # Space Complexity: ? + # Time Complexity: O(1) + # Space Complexity: O(1) def get_first(self): if self.head == None: @@ -25,15 +25,15 @@ def get_first(self): # 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): self.head = Node(value, self.head) # 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): current = self.head while current != None: @@ -43,8 +43,8 @@ def search(self, value): 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): current = self.head count = 0 @@ -56,8 +56,8 @@ 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 = self.head #initialise temp count = 0 #index of current node @@ -70,8 +70,8 @@ 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(1) + # Space Complexity: O(1) def get_last(self): current = self.head if self.head == None: @@ -81,8 +81,8 @@ def get_last(self): return current.value # method that inserts a given value as a new last node in the linked list - # Time Complexity: O(n) - # Space Complexity: ? + # Time Complexity: O(1) + # Space Complexity: O(1) def add_last(self, value): new_node = Node(value) if self.head == None: @@ -95,6 +95,7 @@ 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): head = self.head if (self.head == None): @@ -103,27 +104,33 @@ def find_max(self): max = head.value while (head != None): - if (max < head.value): + if head.value > max: max = head.value head = head.next + return max #print("Maximum value node in the list: " + str(max)) # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? - def delete(self, target_node_data): + # Time Complexity: O(n) + # Space Complexity: O(1) + def delete(self, value): if self.head is None: - raise Exception("List is Empty") - if self.head.valuecl == target_node_data: + return + + if self.head.value == value: self.head = self.head.next + return - previous_node = self.head - for node in self: - if node.data == target_node_data: - previous_node.next = node.next - return - previous_node = node - return None + current = self.head + current = self.head + while current.next is not None: + if value == current.next.value: + break + current = current.next + if current.next is None: + print("Node not found") + else: + current.next = current.next.next # method to print all the values in the linked list # Time Complexity: ?