From 1e796fb8e3cb855ed64d8b0b69f860f970e3b3d1 Mon Sep 17 00:00:00 2001 From: Alma Date: Mon, 28 Mar 2022 23:41:21 -0700 Subject: [PATCH 1/2] working on the problems --- linked_list/linked_list.py | 120 ++++++++++++++++++++++++++----- linked_list/ll.py | 143 +++++++++++++++++++++++++++++++++++++ 2 files changed, 247 insertions(+), 16 deletions(-) create mode 100644 linked_list/ll.py diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..cb2d5275 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -9,14 +9,18 @@ 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 + self.tail = None # 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 None: + return None + + return self.head.value # method to add a new node with the specific data value in the linked list @@ -24,20 +28,35 @@ def get_first(self): # Time Complexity: ? # Space Complexity: ? def add_first(self, value): - pass + if self.head is None: + self.head = self.tail = Node(value) + else: + new_node = Node(value, self.head) + self.head.previous = new_node + 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 + 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: ? def length(self): - pass + temp = self.head + count = 0 + while temp: + count += 1 + temp = temp.next + return count # method that returns the value at a given index in the linked list # index count starts at 0 @@ -45,31 +64,77 @@ def length(self): # Time Complexity: ? # Space Complexity: ? def get_at_index(self, index): - pass + if index < 0: + return None + else: + current = self.head + count = 0 + while current: + if count == index: + return current.value + current = current.next + count += 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: ? def get_last(self): - pass + if self.head == None: + return None + + current = self.head + + while current.next != None: + current = current.next + + return current.value + # 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) + if self.head is None: + self.head = self.tail = new_node + else: + self.tail.next = new_node + new_node.previous = self.tail + self.tail = 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 + current = self.head + max_value = current.value + while current: + if current.value > max_value: + max_value = current.value + current = current.next + return max_value # method to delete the first node found with specified value # Time Complexity: ? # Space Complexity: ? def delete(self, value): - pass + temp = self.head + if temp is not None: + if temp.value == value: + self.head = temp.next + temp.next = None + return + while temp is not None: + if temp.value == value: + break + prev = temp + temp = temp.next + if temp is None: + return + prev.next = temp.next + temp = None + # method to print all the values in the linked list # Time Complexity: ? @@ -86,11 +151,34 @@ 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 - + #start with egdge cases. only one node or no node + # if self.head == self.tail: + # return + + # current = self.head + + # while current is not None: + # prev = current.prev + # next = current.next + # current.next = prev + # current.prev = next + # current = next + + # temp = self.head + # self.head = self.tail + # self.tail = temp + prev = None + current = self.head + while current: + next = current.next + current.next = prev + prev = current + current = next + self.head = prev + ## Advanced/ Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? diff --git a/linked_list/ll.py b/linked_list/ll.py new file mode 100644 index 00000000..1c8f0cbe --- /dev/null +++ b/linked_list/ll.py @@ -0,0 +1,143 @@ +# class Node: +# def __init__(self, value, next=None): +# self.data = value +# self.data = next +# # it references the + +# # add a node to the front of the list +# def add_first(current_head, new_value): +# new_node= Node(new_value) +# new_node.next = head +# return new_node + +# # make a function to get the last element of a linked list +# def get_last(head): +# # edge case +# if head == None: +# return None + +# current = head +# while current.next != None: +# current = current.next + +# return current.data + +# head = Node("Pizza") +# other_node = Node("Pasta") + +# head.next = other_node + +# ther_node = Node('Dumplings') + +# head.next.next = other_node + +# # how to print +# current = head + +# while current != None: +# print(current.data) +# current = current.next + +# # add a node to the front of the list +# new_node = Node('Tacos') +# new_node.next = head +# head = new_node + +# head = add_first(head, 'Burrito') + +# print('---------') +# while current != None: +# print(current.data) +# current = current.next + +# # ****************************************************** +# # encapsulation +# class Node: +# def __init__(self, value, next=None): +# self.data = value +# self.data = next + + +# class LinkedList: +# def __init__(self): +# self.head = None + +# def add_first(self, value): +# # new_node = Node(value) + +# # new_node.next = self.head +# # self.head = new_node +# # orrrr +# self.head = Node(value, self.head) + +# def get_first(self): +# if self.head == None: +# return None + +# return self.head.data + +# def add_last(self, value): +# pass + +# def get_last(self): +# if self.head == None: +# return None + +# current = self.head + +# while current.next != None: +# current = current.next + +# return current.data + +# def find(self, value): +# current = self.head + +# while current != None: +# if current.data == value: +# return True +# current = current.next +# return False + +# ****************************************************** below is my notes +class Node: + def __init__(self, value, next=None): + self.data = value + self.next = next + +def add_first(current_head, new_value): + new_node = Node(new_value) + new_node.next = current_head + return new_node + +def get_last(head): + if head == None: + return None + current = head + while current.next != None: + current = current.next + + return current.data + +head = Node('pizza') +other_node = Node('pasta') + +head.next = other_node + +other_node = Node('Dumplings') + + +head.next.next = other_node +#the code above links the nodes together + + +current = head +# the code below and above are how you print +while current != None: + print(current.data) + current = current.next + +# how do you add a node to the front of the list +new_node = Node('Tacos') +new_node.next = head +head = new_node \ No newline at end of file From d3f0b103dbdf19cf9c1e166b0eeb475f450ef494 Mon Sep 17 00:00:00 2001 From: Alma Date: Sun, 29 May 2022 22:31:26 -0700 Subject: [PATCH 2/2] problem solving --- linked_list/linked_list.py | 67 +++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index cb2d5275..8683ea37 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -25,8 +25,8 @@ def get_first(self): # 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): if self.head is None: self.head = self.tail = Node(value) @@ -37,8 +37,8 @@ def add_first(self, value): # 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): current = self.head while current != None: @@ -48,8 +48,8 @@ def search(self, value): 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): temp = self.head count = 0 @@ -61,8 +61,8 @@ def length(self): # 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^2) + # Space Complexity: O(1) def get_at_index(self, index): if index < 0: return None @@ -78,8 +78,8 @@ def get_at_index(self, index): # 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): if self.head == None: return None @@ -93,8 +93,8 @@ def get_last(self): # 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): new_node = Node(value) if self.head is None: @@ -108,16 +108,25 @@ def add_last(self, value): # returns the data value and not the node def find_max(self): current = self.head - max_value = current.value - while current: - if current.value > max_value: - max_value = current.value + if (self.head == None): + return None + else: + # initializing max to the first node value in the linked list + max = self.head.value + print(max) + while (current != None): + # if the current node's value is greater than the max + # then, replace the value of max_value with the current node's value + if max < current.value: + max = current.value current = current.next - return max_value + return max + + # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n^2) + # Space Complexity: O(1) def delete(self, value): temp = self.head if temp is not None: @@ -137,8 +146,8 @@ def delete(self, value): # 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 @@ -154,22 +163,6 @@ def visit(self): # Time Complexity: O(n) # Space Complexity: O(1) def reverse(self): - #start with egdge cases. only one node or no node - # if self.head == self.tail: - # return - - # current = self.head - - # while current is not None: - # prev = current.prev - # next = current.next - # current.next = prev - # current.prev = next - # current = next - - # temp = self.head - # self.head = self.tail - # self.tail = temp prev = None current = self.head while current: