From 9210c2d7da4834fd51b6adcaa3ee09df80350c88 Mon Sep 17 00:00:00 2001 From: charlottea Date: Sun, 23 Aug 2020 22:25:40 -0700 Subject: [PATCH 1/7] 7 failures --- lib/linked_list.rb | 115 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 96 insertions(+), 19 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..b3965ef7 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,24 +18,49 @@ 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: 0(1) + # Space Complexity: 0(1) def add_first(value) - raise NotImplementedError + new_head = Node.new(3) + new_head.next = @head + @head = new_head + + end # method to find if the linked list contains a node with specified value # returns true if found, false otherwise - # Time Complexity: ? + # Time Complexity: # Space Complexity: ? def search(value) - raise NotImplementedError + return false if @head.nil? + + current = @head + while (current.next != nil) + current = current.next + if current.data.next == value + return true + end + end + return false end # method to return the max value in the linked list # returns the data value and not the node def find_max - raise NotImplementedError + if @head.nil? + return nil + end + current = @head + max = @head.data + + while (current.next != nil) + current = current.next + if current.data > max + max = current.data + end + end + return max end # method to return the min value in the linked list @@ -43,7 +68,17 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min - raise NotImplementedError + if @head.nil? + return nil + end + min = @head.data + while (current.next != nil) + current = current.next + if current.data < min + min = current.data + end + end + return min end @@ -51,7 +86,17 @@ def find_min # Time Complexity: ? # Space Complexity: ? def length - raise NotImplementedError + return 0 if @head.nil? + + current_node = @head + count = 0 + + while (!current_node.nil?) + current_node = current_node.next + count += 1 + end + + return count end # method that returns the value at a given index in the linked list @@ -60,21 +105,49 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - raise NotImplementedError + length = 0 + if index < length + return + end end # method to print all the values in the linked list # Time Complexity: ? # Space Complexity: ? def visit - raise NotImplementedError + if @head.nil? + return + end + + current_node = @head + puts current_node + while (current_node = current_node.next) + puts current_node + end end # method to delete the first node found with specified value # Time Complexity: ? # Space Complexity: ? def delete(value) - raise NotImplementedError + # if head is nil - return + if @head.nil? + return + end + # delete head if needed + if @head.data == value + @head = @head.next + return + end + + current_node = @head + while (current_node.next != nil ) + if current_node.next.data == value + current_node.next = current_node.next.next + return + end + current_node = current_node.next + end end # method to reverse the singly linked list @@ -82,7 +155,14 @@ def delete(value) # Time Complexity: ? # Space Complexity: ? def reverse - raise NotImplementedError + previous_node = nil + while (!@head.nil?) + @head = @head.next + @head.next = previous_node + previous_node = @head + @head = @head.next + end + return previous_node end @@ -91,7 +171,6 @@ def reverse # Time Complexity: ? # Space Complexity: ? def find_middle_value - raise NotImplementedError end # find the nth node from the end and return its value @@ -99,7 +178,6 @@ def find_middle_value # Time Complexity: ? # Space Complexity: ? def find_nth_from_end(n) - raise NotImplementedError end # checks if the linked list has a cycle. A cycle exists if any node in the @@ -108,7 +186,6 @@ def find_nth_from_end(n) # Time Complexity: ? # Space Complexity: ? def has_cycle - raise NotImplementedError end @@ -118,14 +195,14 @@ def has_cycle # Time Complexity: ? # Space Complexity: ? def get_first - raise NotImplementedError + 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 + end # method that returns the value of the last node in the linked list @@ -133,7 +210,7 @@ def add_last(value) # Time Complexity: ? # Space Complexity: ? def get_last - raise NotImplementedError + end # method to insert a new node with specific data value, assuming the linked @@ -141,7 +218,7 @@ def get_last # Time Complexity: ? # Space Complexity: ? def insert_ascending(value) - raise NotImplementedError + end # Helper method for tests From e0981b22c642f84d22c915cdb99fd5aa1f73e177 Mon Sep 17 00:00:00 2001 From: charlottea Date: Sun, 23 Aug 2020 23:02:12 -0700 Subject: [PATCH 2/7] 2 failures, 2 errors --- lib/linked_list.rb | 121 ++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 61 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index b3965ef7..54adcd12 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,13 +21,10 @@ def initialize # Time Complexity: 0(1) # Space Complexity: 0(1) def add_first(value) - new_head = Node.new(3) - new_head.next = @head - @head = new_head - - + @head = Node.new(value, @head) + return @head end - + # method to find if the linked list contains a node with specified value # returns true if found, false otherwise # Time Complexity: @@ -36,13 +33,12 @@ def search(value) return false if @head.nil? current = @head - while (current.next != nil) + while (current.next != nil && current.data != value) do current = current.next - if current.data.next == value - return true - end end - return false + + + return end # method to return the max value in the linked list @@ -51,8 +47,7 @@ def find_max if @head.nil? return nil end - current = @head - max = @head.data + while (current.next != nil) current = current.next @@ -68,10 +63,13 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min + current = @head + min = @head.data if @head.nil? return nil end - min = @head.data + + while (current.next != nil) current = current.next if current.data < min @@ -86,17 +84,16 @@ def find_min # Time Complexity: ? # Space Complexity: ? def length - return 0 if @head.nil? - current_node = @head - count = 0 - + length = 0 + return 0 if @head.nil? + while (!current_node.nil?) current_node = current_node.next - count += 1 + length += 1 end - return count + return length end # method that returns the value at a given index in the linked list @@ -105,26 +102,25 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - length = 0 - if index < length - return + return nil if @head.nil? + current = @head + index.times do + return nil if current.next.nil? + current = current.next end + return current.data end # method to print all the values in the linked list # Time Complexity: ? # Space Complexity: ? def visit - if @head.nil? - return - end - - current_node = @head - puts current_node - while (current_node = current_node.next) - puts current_node + current = @head + if current = current.data + return current.data end end + # method to delete the first node found with specified value # Time Complexity: ? @@ -155,38 +151,41 @@ def delete(value) # Time Complexity: ? # Space Complexity: ? def reverse + return if @head.nil? + current = @head previous_node = nil - while (!@head.nil?) - @head = @head.next - @head.next = previous_node - previous_node = @head - @head = @head.next + while (!current.nil?) + temp = current.next + current.next = previous_node + previous_node = current + current = temp end - return previous_node + @head = previous_node end + end ## Advanced Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? # Space Complexity: ? - def find_middle_value - end + # def find_middle_value + # end # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n # Time Complexity: ? # Space Complexity: ? - def find_nth_from_end(n) - end + # def find_nth_from_end(n) + # end # 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 - end + # def has_cycle + # end # Additional Exercises @@ -194,45 +193,45 @@ def has_cycle # returns nil if the list is empty # Time Complexity: ? # Space Complexity: ? - def get_first + # def get_first - end + # end # method that inserts a given value as a new last node in the linked list # Time Complexity: ? # Space Complexity: ? - def add_last(value) + # def add_last(value) - end + # end # method that returns the value of the last node in the linked list # returns nil if the linked list is empty # Time Complexity: ? # Space Complexity: ? - def get_last + # def get_last - end + # end # method to insert a new node with specific data value, assuming the linked # list is sorted in ascending order # Time Complexity: ? # Space Complexity: ? - def insert_ascending(value) + # def insert_ascending(value) - end + # end # 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 - return if @head == nil # don't do anything if the linked list is empty + # def create_cycle + # return if @head == nil # don't do anything if the linked list is empty # navigate to last node - current = @head - while current.next != nil - current = current.next - end - - current.next = @head # make the last node link to first node - end -end +# current = @head +# while current.next != nil +# current = current.next +# end + +# current.next = @head # make the last node link to first node +# end +# end From 2060c0302d39b2dd7efc48a5e6f54a954227d8a5 Mon Sep 17 00:00:00 2001 From: charlottea Date: Mon, 24 Aug 2020 20:55:36 -0700 Subject: [PATCH 3/7] two failures, two errors --- lib/linked_list.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 54adcd12..2737c972 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -63,8 +63,6 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min - current = @head - min = @head.data if @head.nil? return nil end From d081d35be96281e505eb1f8c75fac841d3f9e29a Mon Sep 17 00:00:00 2001 From: charlottea Date: Mon, 24 Aug 2020 21:19:31 -0700 Subject: [PATCH 4/7] search method pass, 1 failure, 1 error --- lib/linked_list.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 2737c972..0b66515f 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -35,20 +35,20 @@ def search(value) current = @head while (current.next != nil && current.data != value) do current = current.next + if current.data != value + return false + end end - - - return + return true end # method to return the max value in the linked list # returns the data value and not the node def find_max - if @head.nil? - return nil - end - + return nil if @head.nil? + current = @head + max = 0 while (current.next != nil) current = current.next if current.data > max @@ -63,11 +63,11 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min - if @head.nil? - return nil - end - - + return nil if @head.nil? + + + current = @head + min = 0 while (current.next != nil) current = current.next if current.data < min From d5d414c360dce828fb60a0245509a1c68e163b74 Mon Sep 17 00:00:00 2001 From: charlottea Date: Mon, 24 Aug 2020 21:21:47 -0700 Subject: [PATCH 5/7] find_min, find_max pass --- lib/linked_list.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0b66515f..0855e972 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -48,7 +48,7 @@ def find_max return nil if @head.nil? current = @head - max = 0 + max = current.data while (current.next != nil) current = current.next if current.data > max @@ -67,7 +67,7 @@ def find_min current = @head - min = 0 + min = current.data while (current.next != nil) current = current.next if current.data < min From fdae59d4a61056783e8388339f7cc49fc1180299 Mon Sep 17 00:00:00 2001 From: charlottea Date: Mon, 24 Aug 2020 23:13:24 -0700 Subject: [PATCH 6/7] add big0 notation --- lib/linked_list.rb | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0855e972..5999c807 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -27,8 +27,8 @@ def add_first(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:0(n) with n being the length of the list + # Space Complexity:0(n) def search(value) return false if @head.nil? @@ -46,8 +46,8 @@ def search(value) # returns the data value and not the node def find_max return nil if @head.nil? + current = @head - max = current.data while (current.next != nil) current = current.next @@ -60,12 +60,11 @@ def find_max # method to return the min value in the linked list # returns the data value and not the node - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: 0(n) with n being the length of the list + # Space Complexity: 0(n) def find_min return nil if @head.nil? - current = @head min = current.data while (current.next != nil) @@ -79,8 +78,8 @@ def find_min # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: 0(n) with n being the length + # Space Complexity: 0(n) def length current_node = @head length = 0 @@ -97,8 +96,8 @@ def length # 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: 0(n) with n being the length of the list + # Space Complexity: 0(n) def get_at_index(index) return nil if @head.nil? current = @head @@ -110,8 +109,8 @@ def get_at_index(index) end # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: 0(n) with n being the length of the list + # Space Complexity: 0(n) def visit current = @head if current = current.data @@ -121,7 +120,7 @@ def visit # method to delete the first node found with specified value - # Time Complexity: ? + # Time Complexity: 0(n) with n being the length of the list # Space Complexity: ? def delete(value) # if head is nil - return @@ -146,8 +145,8 @@ def delete(value) # 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: 0(n) n being the length of the list + # Space Complexity: 0(n) def reverse return if @head.nil? current = @head From 71c5a1b3e0a8b6c63de8f756e3b6596b509b87bf Mon Sep 17 00:00:00 2001 From: charlottea Date: Sun, 30 Aug 2020 10:40:58 -0700 Subject: [PATCH 7/7] comments --- lib/linked_list.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5999c807..ac7209c9 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -20,10 +20,15 @@ def initialize # insert the new node at the beginning of the linked list # Time Complexity: 0(1) # Space Complexity: 0(1) - def add_first(value) - @head = Node.new(value, @head) - return @head - end + + def add_first(value) + @head = Node.new(value, @head) + return @head + end + # new_node = Node.new(value) + # new_node.next = @head + # @head = new_node + # method to find if the linked list contains a node with specified value # returns true if found, false otherwise