From a527ff3e64fc9439208e2113871f8c5a98bd80ca Mon Sep 17 00:00:00 2001 From: Sandra Caballero Date: Thu, 9 Jun 2022 11:37:30 -0700 Subject: [PATCH 1/3] specified node --- linked_list/linked_list.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 63993214..3ed4cf31 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -13,10 +13,13 @@ def __init__(self): # returns the value in the first node # returns None if the list is empty - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(1) accessing it only on + # Space Complexity: O(1) no new data structure being created 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 @@ -24,8 +27,8 @@ def get_first(self): # Time Complexity: ? # Space Complexity: ? def add_first(self, value): - pass - + self.head = Node(value,None) + # method to find if the linked list contains a node with specified value # returns true if found, false otherwise # Time Complexity: ? From 255e82d055314c1c9fd8152ff109006940150e07 Mon Sep 17 00:00:00 2001 From: Sandra Caballero Date: Thu, 9 Jun 2022 12:42:42 -0700 Subject: [PATCH 2/3] length of ll --- linked_list/linked_list.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index 3ed4cf31..ebe6df83 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -21,26 +21,38 @@ def get_first(self): 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(1) + # Space Complexity: o(1) constant time, since new data structure is being created but its small def add_first(self, value): - self.head = Node(value,None) + 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: ? def search(self, value): - pass + current = self.head + while current is not None: + if value == current.value: + return True + current = current.next +# reset current to equal the next node in the list + return False # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: 0(1) constant time + # Space Complexity: 0(1) def length(self): - pass + current = self.head + length = 0 + + while current != None: + length += 1 + current = current.next + + return length # method that returns the value at a given index in the linked list # index count starts at 0 @@ -48,15 +60,16 @@ def length(self): # Time Complexity: ? # Space Complexity: ? def get_at_index(self, index): - pass + if self.length() < index: + 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 - + + # method that inserts a given value as a new last node in the linked list # Time Complexity: ? # Space Complexity: ? From b7cc8bb9137d5f4d5e02f1a447f1f3041192b182 Mon Sep 17 00:00:00 2001 From: Sandra Caballero Date: Thu, 30 Jun 2022 20:06:46 -0700 Subject: [PATCH 3/3] linked lists --- linked_list/linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linked_list/linked_list.py b/linked_list/linked_list.py index ebe6df83..d9532936 100644 --- a/linked_list/linked_list.py +++ b/linked_list/linked_list.py @@ -68,7 +68,7 @@ def get_at_index(self, index): # Time Complexity: ? # Space Complexity: ? def get_last(self): - + pass # method that inserts a given value as a new last node in the linked list # Time Complexity: ?