From 5713826bc7e904914aca77cc279a811645d1d9d5 Mon Sep 17 00:00:00 2001 From: Andrea Yuson Date: Wed, 13 Jul 2022 12:14:53 -0700 Subject: [PATCH] Linked List CS Fun Project --- linked_list/linked_list.py | 171 +++++++++++++++++++++++++++++++++---- 1 file changed, 155 insertions(+), 16 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..175e4351 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -13,31 +13,61 @@ def __init__(self): # returns the value in the first node # returns None if the list is empty - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: 0(1) + # Space Complexity: 0(n) def get_first(self): - pass + if self.head == None: + return None +#The other scenario would mean there is a value in the first node or the 1st node exists. + 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: ? def add_first(self, value): - pass + if self.head == None: + new_node = Node(value=value, next_node=None) + self.head= new_node + else: + new_node = Node(value=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: ? def search(self, value): - pass + if self.head == None: + return False + else: + current = self.head + while current != None: + if current.value == value: + return True + else: + current = current.next + return False + # method that returns the length of the singly linked list # Time Complexity: ? # Space Complexity: ? def length(self): - pass + if self.head == None: + return 0 + else: + counter = 0 + current = self.head + while current != None: + counter += 1 + current = current.next + return counter + # method that returns the value at a given index in the linked list # index count starts at 0 @@ -45,31 +75,82 @@ def length(self): # Time Complexity: ? # Space Complexity: ? def get_at_index(self, index): - pass + if self.head == None: + return None + else: + counter = 0 + current = self.head + while current != None: + if counter == index: + return current.value + else: + counter += 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 + if self.head == None: + return None + else: + current = self.head + while current != None: + if current.next == None: + return current.value + else: + 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): - pass + new_node = Node(value, next_node=None) + if self.head == None: + self.head = new_node + else: + current = self.head + while current != None: + if current.next == None: + new_node = Node(value, next_node=None) + current.next = new_node + current = current.next + return + else: + 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 self.head == None: + return None + else: + final_max = self.head.value + current = self.head.next + while current != None: + if current.value > final_max: + final_max = current.value + current = current.next + return final_max # method to delete the first node found with specified value # Time Complexity: ? # Space Complexity: ? def delete(self, value): - pass + if self.head == None: + return None + elif self.head.value == value: + self.head = self.head.next + else: + current = self.head + while current.next != None: + if current.next.value == value: + current.next = current.next.next + return + else: + current = current.next # method to print all the values in the linked list # Time Complexity: ? @@ -89,21 +170,68 @@ def visit(self): # Time Complexity: ? # Space Complexity: ? def reverse(self): - pass + if self.head == None: + return + elif self.head.next == None: + return + else: + current = self.head + reversed_link = None + while current.next != None: + if not reversed_link: + next_node = current.next + current.next = None + reversed_link = current + current = next_node + else: + next_node = current.next + current.next = reversed_link + reversed_link = current + current = next_node + current.next = reversed_link + reversed_link = current + self.head = reversed_link + return self ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? # Space Complexity: ? def find_middle_value(self): - pass + if self.head == None: + return None + elif self.head.next == None: + return self.head.value + else: + one_step_current = self.head + two_step_current = self.head + while two_step_current != None and two_step_current.next != None: + one_step_current = one_step_current.next + two_step_current = two_step_current.next.next + return one_step_current.value # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n # Time Complexity: ? # Space Complexity: ? def find_nth_from_end(self, n): - pass + if self.head == None: + return None + else: + one_step_current = self.head + n_step_current = self.head + counter = 0 + while n_step_current.next != None: + if counter < n: + n_step_current = n_step_current.next + counter += 1 + else: + n_step_current = n_step_current.next + one_step_current = one_step_current.next + if counter < n: + return None + else: + return one_step_current.value # checks if the linked list has a cycle. A cycle exists if any node in the # linked list links to a node already visited. @@ -111,7 +239,18 @@ def find_nth_from_end(self, n): # Time Complexity: ? # Space Complexity: ? def has_cycle(self): - pass + if self.head == None: + return False + else: + node_set = set() + current = self.head + while current != None: + if current not in node_set: + node_set.add(current) + current = current.next + else: + return True + return False # Helper method for tests # Creates a cycle in the linked list for testing purposes @@ -125,4 +264,4 @@ def create_cycle(self): while current.next != None: current = current.next - current.next = self.head # make the last node link to first node + current.next = self.head # make the last node link to first node \ No newline at end of file