diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..170f661c 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -1,75 +1,135 @@ - -# Defines a node in the singly linked list class Node: + ''' + Defines a node in the singly linked list + ''' - def __init__(self, value, next_node = None): + def __init__(self, value, next_node=None): self.value = value self.next = next_node -# Defines the singly linked list + class LinkedList: + ''' + Defines the singly linked list + ''' + 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 is not None: + return self.head.value + else: + return None # 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 + self.head = Node(value, next_node=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 + current_node = self.head + 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def length(self): - pass + current_node = self.head + length = 0 + while current_node is not None: + length += 1 + current_node = current_node.next + 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 + current_node = self.head + current_index = 0 + while current_node is not None: + if index == current_index: + return current_node.value + 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 - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_last(self): - pass + current_node = self.head + while current_node is not None: + if current_node.next is None: + return current_node.value + current_node = current_node.next + return None # 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 + last_node = self.head + while last_node is not None: + if last_node.next is None: + break + last_node = last_node.next + + if last_node is not None: + last_node.next = Node(value) + else: + self.head = 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 + current_node = self.head + if current_node is None: + return None + max_val = current_node.value + while current_node is not None: + max_val = max(current_node.value, max_val) + current_node = current_node.next + return max_val # 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 + # Head has the value to delete + if self.head is not None\ + and self.head.value == value: + self.head = self.head.next + return + + current_node = self.head + # value to delete is in not head + while current_node is not None: + next_node = current_node.next + if next_node is not None\ + and next_node.value == value: + current_node.next = next_node.next + return + current_node = next_node # method to print all the values in the linked list # Time Complexity: ? @@ -81,20 +141,36 @@ 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 # 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): - pass - + node_list = [] + current_node = self.head + while current_node is not None: + node_list.append(current_node) + current_node = current_node.next + + for index in range(len(node_list)-1, 0, -1): + try: + node_list[index].next = node_list[index-1] + except IndexError: + node_list[index].next = None + + try: + self.head = node_list[-1] + except IndexError: + self.head = None + ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? # Space Complexity: ? + def find_middle_value(self): pass @@ -125,4 +201,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