diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..55e9d66a 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -1,5 +1,8 @@ # Defines a node in the singly linked list +from calendar import c + + class Node: def __init__(self, value, next_node = None): @@ -9,71 +12,150 @@ 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 + 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 + # get head and iterate through nodes until right value + current_node = self.head + if not current_node: + return False + + while current_node.value != value: + if current_node.next: + current_node = current_node.next + else: + return False + else: + return True # 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 + # start counter, go through list, if next increase counter + current_node = self.head + length = 0 + while current_node: + length += 1 + if current_node.next: + current_node = current_node.next + else: + return length + else: + return length + # 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 + # use counter, when counter matches index, return + current_node = self.head + counter = 0 + if not current_node: + return None + elif counter == index: + return current_node.value + while counter != index: + if current_node.next: + current_node = current_node.next + else: + return None + counter += 1 + else: + return current_node.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(1) ?? def get_last(self): - pass + current_node = self.head + if not current_node: + return None + while current_node.next: + current_node = current_node.next + else: + return current_node.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 + current_node = self.head + new_node = Node(value) + if not current_node: + self.head = new_node + return + # self.add_first(value) + # return + while current_node.next: + current_node = current_node.next + else: + current_node.next = new_node # 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 + current_node = self.head + if not current_node: + return None + current_max = -900000000 + while current_node != None: + if current_max < current_node.value: + current_max = current_node.value + current_node = current_node.next + return current_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 + current_node = self.head + if not current_node: + return None + while current_node: + if current_node.value == value: + self.head = current_node.next + break + elif current_node.next.value == value: + current_node.next = current_node.next.next + break + else: + current_node = current_node.next # 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 @@ -86,11 +168,22 @@ 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 - + current_node = self.head + previous_node = None + next_node = current_node.next + + while current_node: + current_node.next = previous_node + previous_node = current_node + current_node = next_node + if next_node: + next_node = next_node.next + + self.head = previous_node + ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ?