From 8714af80d1b37be263c55a2f206425fd6ef23f16 Mon Sep 17 00:00:00 2001 From: Sophie Date: Thu, 18 Mar 2021 21:13:54 -0700 Subject: [PATCH 1/2] initial commit --- lib/linked_list.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..63a51593 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,7 +21,8 @@ def initialize # Time Complexity: ? # Space Complexity: ? def add_first(value) - raise NotImplementedError + new_node = Node.new(value) + @head = new_node end # method to find if the linked list contains a node with specified value @@ -29,13 +30,29 @@ def add_first(value) # Time Complexity: ? # Space Complexity: ? def search(value) - raise NotImplementedError + + current_node = @head + + until current_node.next == nil + if current_node.data == value + return true + else + current_node.data = current_node.next + end + + end + end # method to return the max value in the linked list # returns the data value and not the node def find_max - raise NotImplementedError + current_node = @head + current_value = current_node.data + + until current_node.next == nil + current_value = + end end # method to return the min value in the linked list From a6294c296713dda2fa15127c02d11402117cfe71 Mon Sep 17 00:00:00 2001 From: Sophie Date: Sat, 20 Mar 2021 15:34:59 -0700 Subject: [PATCH 2/2] all implemented methods passing --- lib/linked_list.rb | 111 +++++++++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 30 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 63a51593..23a4b128 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,82 +18,133 @@ def initialize # 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(value) + + # if head is nil + return @head = Node.new(value) if @head.nil? + + # if head contains data new_node = Node.new(value) + new_node.next = @head @head = new_node + return @head end # 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(value) current_node = @head - until current_node.next == nil - if current_node.data == value - return true - else - current_node.data = current_node.next - end - + while current_node + return true if current_node.data == value + current_node = current_node.next end - + return false end # method to return the max value in the linked list # returns the data value and not the node + # Time Complexity: o(n) + # Space Complexity: o(1) def find_max + return nil if @head.nil? + current_node = @head - current_value = current_node.data - - until current_node.next == nil - current_value = - end + max = 0 + + while current_node + if current_node.data > max + max = current_node.data + current_node = current_node.next + else + current_node = current_node.next + end + end + return max end # method to return the min value in the linked list # returns the data value and not the node - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: o(n) + # Space Complexity: o(1) def find_min - raise NotImplementedError + return nil if @head.nil? + current_node = @head + min = current_node.data + + while current_node + if current_node.data < min + min = current_node.data + current_node = current_node.next + else + current_node = current_node.next + end + end + return min end # Additional Exercises # returns the value in the first node # returns nil if the list is empty - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: o(1) + # Space Complexity: o(1) def get_first - raise NotImplementedError + return @head ? @head.data : nil end # method that inserts a given value as a new last node in the linked list # Time Complexity: ? # Space Complexity: ? def add_last(value) - raise NotImplementedError + last_node = Node.new(value) + + if @head.nil? + @head = last_node + else + current_node = @head + until current_node.next.nil? + current_node = current_node.next + end + current_node.next = last_node + end end # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: o(n) + # Space Complexity: o(1) def length - raise NotImplementedError + current_node = @head + count = 0 + + while current_node + count += 1 + current_node = current_node.next + end + return count end # method that returns the value at a given index in the linked list # index count starts at 0 # returns nil 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(index) - raise NotImplementedError + + current_node = @head + count = 0 + + while current_node + return current_node.data if count == index + current_node = current_node.next + count += 1 + end + return nil end # method to print all the values in the linked list