From 4dfe4c4f3504f720004b29c3ae421e9e5d921285 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 24 Aug 2020 15:14:00 -0700 Subject: [PATCH 1/4] need to get used to committing again --- lib/linked_list.rb | 86 +++++++++++++++++++++++++++++++++------- test/linked_list_test.rb | 2 +- 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..4dc30c88 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,10 +18,10 @@ 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: o1 + # Space Complexity: o1 def add_first(value) - raise NotImplementedError + @head = Node.new(value, @head) end # method to find if the linked list contains a node with specified value @@ -34,33 +34,72 @@ def search(value) # 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 - raise NotImplementedError + return nil if @head == nil + + current = @head + max_value = @head.data + + if current.data >= max + max_value = current.data + current = current.next + end + + return max_value 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 = @head + min_value = @head.data + + if current.data <= min + min_value = current.data + current = current.next + end + + return min_value end # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: on + # Space Complexity: o1 def length - raise NotImplementedError + current = @head + count = 0 + + while current != nil + count += 1 + current = current.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: on + # Space Complexity: o1 def get_at_index(index) - raise NotImplementedError + current = @head + count = 0 + + until current.nil? + return current.data if count == index + current = current.next + count += 1 + end + + return nil end # method to print all the values in the linked list @@ -118,14 +157,24 @@ def has_cycle # Time Complexity: ? # Space Complexity: ? def get_first - raise NotImplementedError + return nil if head.nil? + return head.data 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 + return nil if @head == nil + + current = @head + + return self.add_first(data) if current == nil + while current.next != nil + current = current.next + end + + current.next = Node.new(value) end # method that returns the value of the last node in the linked list @@ -133,6 +182,13 @@ def add_last(value) # Time Complexity: ? # Space Complexity: ? def get_last + current = @head + + while current.next != nil + current = current.next + end + + return current.data raise NotImplementedError end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index c1756935..87accc12 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -192,7 +192,7 @@ end end - xdescribe "Optional: nth_from_the_end" do + describe "Optional: nth_from_the_end" do it 'returns nil if n is outside the bounds of the list' do expect(@list.find_nth_from_end(3)).must_be_nil end From ec21cf2ca06b4d4de7d1ac25e509947b5ddca0b6 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 24 Aug 2020 15:25:58 -0700 Subject: [PATCH 2/4] added search, 5 errors left --- lib/linked_list.rb | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 4dc30c88..a11714cf 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -29,7 +29,16 @@ def add_first(value) # Time Complexity: ? # Space Complexity: ? def search(value) - raise NotImplementedError + current = @head + + while current + if current.data == value + return true + end + current = current.next + end + + return false end # method to return the max value in the linked list @@ -40,14 +49,16 @@ def find_max return nil if @head == nil current = @head - max_value = @head.data + max = @head.data - if current.data >= max - max_value = current.data + while current + if current.data > max + max = current.data + end current = current.next end - return max_value + return max end # method to return the min value in the linked list @@ -58,14 +69,16 @@ def find_min return nil if @head == nil current = @head - min_value = @head.data + min = @head.data - if current.data <= min - min_value = current.data + while current + if current.data < min + min = current.data + end current = current.next end - return min_value + return min end @@ -106,7 +119,14 @@ def get_at_index(index) # Time Complexity: ? # Space Complexity: ? def visit - raise NotImplementedError + print @head.data if @head.next == nil + + current = @head + + while current + print current.data + current = current.next + end end # method to delete the first node found with specified value @@ -130,7 +150,9 @@ def reverse # Time Complexity: ? # Space Complexity: ? def find_middle_value - raise NotImplementedError + length = self.length + mid = length/2 + return get_at_index(mid) end # find the nth node from the end and return its value From 3e714aad852563de2e3106defe648b4d966ab2c5 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 24 Aug 2020 15:36:15 -0700 Subject: [PATCH 3/4] delete method, 3 errors --- lib/linked_list.rb | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index a11714cf..beb18935 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -26,8 +26,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: o1 + # Space Complexity: o1 def search(value) current = @head @@ -43,8 +43,8 @@ def search(value) # 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) + #Time Complexity: on + # Space Complexity: o1 def find_max return nil if @head == nil @@ -130,10 +130,25 @@ def visit end # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: on + # Space Complexity: o1 def delete(value) - raise NotImplementedError + return nil if @head.nil? + + if @head.data == value + @head = @head.next + return + end + + previous = @head + current = @head.next + + until current.nil? || current.data == value + previous = current + current = current.next + end + + previous.next = current.next end # method to reverse the singly linked list @@ -179,8 +194,8 @@ def has_cycle # Time Complexity: ? # Space Complexity: ? def get_first - return nil if head.nil? - return head.data + return nil if @head.nil? + return @head.data end # method that inserts a given value as a new last node in the linked list From 68e2211acb2765e442fadb30c4ea7913b3056447 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 24 Aug 2020 15:43:29 -0700 Subject: [PATCH 4/4] all but some optionals, tests pass --- lib/linked_list.rb | 19 ++++++++++++++++--- test/linked_list_test.rb | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index beb18935..9945f85d 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -153,10 +153,23 @@ 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: on + # Space Complexity: o1 def reverse - raise NotImplementedError + return nil if @head == nil + + current = @head + subsequent = nil + previous = nil + + while current + subsequent = current.next + current.next = previous + previous = current + current = subsequent + end + + @head = previous end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 87accc12..c1756935 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -192,7 +192,7 @@ end end - describe "Optional: nth_from_the_end" do + xdescribe "Optional: nth_from_the_end" do it 'returns nil if n is outside the bounds of the list' do expect(@list.find_nth_from_end(3)).must_be_nil end