From e3ece3d074078ec95e8ce8eb199987d5c254c502 Mon Sep 17 00:00:00 2001 From: rpeeples Date: Tue, 7 Jun 2022 22:04:36 -0400 Subject: [PATCH] Ro Linked List Python --- linked_list/linked_list.py | 213 ++++++++++++++++++++++--------------- 1 file changed, 130 insertions(+), 83 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..6d2da764 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -1,79 +1,118 @@ -# Defines a node in the singly linked list + 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 - - # returns the value in the first node - # returns None if the list is empty - # Time Complexity: ? - # Space Complexity: ? + self.head = None def get_first(self): - pass - + if self.head: + return self.head.value + 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, 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(1) + # Space Complexity: O(1) def search(self, value): - pass + current = self.head + + while current: + 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(n) def length(self): - pass + count = 0 + current = self.head + 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 - # 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(n) def get_at_index(self, index): - pass + current_index = 0 + current = self.head + while current_index < index and current: + current_index += 1 + current = current.next + + if current_index == index and current: + return current.value + 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(n) def get_last(self): - pass + if not self.head: + return None + + current = self.head + while current.next: + current = current.next - # method that inserts a given value as a new last node in the linked list - # Time Complexity: ? - # Space Complexity: ? + return current.value + + # Time Complexity: O(n); more than 1 approach; same complexity + # Space Complexity: O(n); more than 1 approach; same complexity def add_last(self, value): - pass + if not self.head: + self.add_first(value) + else: + new_node = Node(value) + current = self.head + while current.next: + current = current.next + current.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 + if self.head == None: + return None + + current = self.head + max = self.head.value + while current: + 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(n) def delete(self, value): - pass + if not self.head: + return + + if value == 0: + self.head = self.head.next + + current = self.head + current_index = 0 + while current.next and current_index < value - 1: + current = current.next + current_index += 1 + + if current.next: + current.next = current.next.next - # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(1) after element is found; O(n) prior + # Space Complexity: O(1) def visit(self): helper_list = [] current = self.head @@ -84,45 +123,53 @@ def visit(self): 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 - - ## Advanced/ Exercises - # returns the value at the middle element in the singly linked list - # Time Complexity: ? - # Space Complexity: ? + if not self.head: + return + + current = self.head + previous = None + + while current: + next = current.next + current.next = previous + previous = current + current = next + + self.head = previous + + # Time Complexity: O(n) + # Space Complexity: O(n) def find_middle_value(self): - pass + if not self.head: + return None + + return self.get_at_index(int(self.length() / 2)) - # find the nth node from the end and return its value - # assume indexing starts at 0 while counting to n - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(n) def find_nth_from_end(self, n): - pass - - # checks if the linked list has a cycle. A cycle exists if any node in the - # linked list links to a node already visited. - # returns true if a cycle is found, false otherwise. - # Time Complexity: ? - # Space Complexity: ? - def has_cycle(self): - pass - - # Helper method for tests - # Creates a cycle in the linked list for testing purposes - # Assumes the linked list has at least one node - def create_cycle(self): - if self.head == None: - return + if not self.head: + return None - # navigate to last node current = self.head - while current.next != None: + trail = None + spacing = 0 + while current and spacing < n: current = current.next + spacing += 1 + + if not current: + return None + + trail = self.head + while current.next: + current = current.next + trail = trail.next + + return trail.value - current.next = self.head # make the last node link to first node + # Time Complexity: O(log n) + # Space Complexity: O(log n)