From 21dc057b63c82fe54193cf4995460dd218973e3f Mon Sep 17 00:00:00 2001 From: Makhabat Date: Sun, 3 Jul 2022 02:10:27 -0700 Subject: [PATCH] Completed the project untill advanced exercises --- linked_list/linked_list.py | 139 +++++++++++++++++++++++++++---------- 1 file changed, 104 insertions(+), 35 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..7389f1d8 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -1,79 +1,136 @@ # Defines a node in the singly linked list -class Node: +from platform import node + - def __init__(self, value, next_node = None): +class Node: + def __init__(self, value, next_node=None): self.value = value self.next = next_node # 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 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) + 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def search(self, value): - pass + if self.head == None: + return False + 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def length(self): - pass + lenght = 0 + current = self.head + while current != None: + lenght += 1 + current = current.next + return lenght # 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 + i = 0 + current = self.head + while current != None: + if i == index: + return current.value + current = current.next + i += 1 + 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_last(self): - pass + if self.head == None: + return None + current = self.head + while current != None: + if current.next == None: + 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) + def add_last(self, value): - pass + if self.head == None: + self.add_first(value) + else: + current = self.head + while current.next != None: + current = current.next + current.next = Node(value) # 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 + current = self.head + max = current.value + while current != None: + if current.value > max: + max = current.value + current = current.next + return 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 + if self.head == None: + return None + if self.head.value == value: + self.head = self.head.next + else: + current = self.head + while current.next.value != value: + current = current.next + current.next = current.next.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 @@ -81,7 +138,7 @@ def visit(self): while current: helper_list.append(str(current.value)) current = current.next - + print(", ".join(helper_list)) # method to reverse the singly linked list @@ -89,8 +146,20 @@ def visit(self): # Time Complexity: ? # Space Complexity: ? def reverse(self): - pass - + if self.head == None: + return None + if self.head.next == None: + return self.head + next_node = self.head.next + self.head.next = None + current = self.head + while (next_node != None): + temp_var = next_node.next + next_node.next = current + current = next_node + next_node = temp_var + self.head = current + ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? @@ -125,4 +194,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