From bcc1aef83ef065953387c460996a55eb97187368 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Thu, 22 Aug 2019 14:04:47 -0700 Subject: [PATCH 01/18] get first and add to front done --- lib/linked_list.rb | 11 +- test/linked_list_test.rb | 326 +++++++++++++++++++-------------------- 2 files changed, 172 insertions(+), 165 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..2c77dabd 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,7 +21,13 @@ def initialize # Time Complexity: # Space Complexity def add_first(value) - raise NotImplementedError + if !@head + @head = Node.new(value) + else + new_node = Node.new(value) + new_node.next = @head + @head = new_node + end end # method to find if the linked list contains a node with specified value @@ -120,7 +126,8 @@ def has_cycle # Time Complexity: # Space Complexity def get_first - raise NotImplementedError + return @head.data if @head + return end # method that inserts a given value as a new last node in the linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index a7ee3769..656b1087 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -69,173 +69,173 @@ end end - describe "addLast & getLast" do - it "will add to the front if the list is empty" do - # Arrange - @list.add_last(1) - # Act-Assert - expect(@list.get_first).must_equal 1 - end - - it "will put new items to the rear of the list" do - # Arrange - @list.add_last(2) - # Act-Assert - expect(@list.length).must_equal 1 - expect(@list.get_last).must_equal 2 - - # Arrange - @list.add_last(3) - # Act-Assert - expect(@list.get_first).must_equal 2 - expect(@list.get_last).must_equal 3 - expect(@list.length).must_equal 2 - - @list.add_last(4) - expect(@list.get_first).must_equal 2 - expect(@list.get_last).must_equal 4 - expect(@list.length).must_equal 3 - end +# describe "addLast & getLast" do +# it "will add to the front if the list is empty" do +# # Arrange +# @list.add_last(1) +# # Act-Assert +# expect(@list.get_first).must_equal 1 +# end + +# it "will put new items to the rear of the list" do +# # Arrange +# @list.add_last(2) +# # Act-Assert +# expect(@list.length).must_equal 1 +# expect(@list.get_last).must_equal 2 + +# # Arrange +# @list.add_last(3) +# # Act-Assert +# expect(@list.get_first).must_equal 2 +# expect(@list.get_last).must_equal 3 +# expect(@list.length).must_equal 2 + +# @list.add_last(4) +# expect(@list.get_first).must_equal 2 +# expect(@list.get_last).must_equal 4 +# expect(@list.length).must_equal 3 +# end - it "will return `nil` for `get_last` if the list is empty" do - # Act-Assert - expect(@list.get_last).must_be_nil - end - end - - describe 'get_at_index' do - it 'returns nil if the index is outside the bounds of the list' do - # Act-Assert - expect(@list.get_at_index(3)).must_be_nil - end - - it 'can retrieve an item at an index in the list' do - # Arrange - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - # Act-Assert - expect(@list.get_at_index(0)).must_equal 4 - expect(@list.get_at_index(1)).must_equal 3 - expect(@list.get_at_index(2)).must_equal 2 - expect(@list.get_at_index(3)).must_equal 1 - end - end - - describe 'max and min values' do - it 'returns nil if the list is empty' do - # Act-Assert - expect(@list.find_max()).must_be_nil - expect(@list.find_min()).must_be_nil - end - - it 'can retrieve the max and min values in the list' do - # Arrange - count = 0 - while count < 5 - @list.add_first(count) - expect(@list.find_max).must_equal count - expect(@list.find_min).must_equal 0 - count += 1 - end - @list.add_last(100) - @list.add_first(-12) +# it "will return `nil` for `get_last` if the list is empty" do +# # Act-Assert +# expect(@list.get_last).must_be_nil +# end +# end + +# describe 'get_at_index' do +# it 'returns nil if the index is outside the bounds of the list' do +# # Act-Assert +# expect(@list.get_at_index(3)).must_be_nil +# end + +# it 'can retrieve an item at an index in the list' do +# # Arrange +# @list.add_first(1) +# @list.add_first(2) +# @list.add_first(3) +# @list.add_first(4) + +# # Act-Assert +# expect(@list.get_at_index(0)).must_equal 4 +# expect(@list.get_at_index(1)).must_equal 3 +# expect(@list.get_at_index(2)).must_equal 2 +# expect(@list.get_at_index(3)).must_equal 1 +# end +# end + +# describe 'max and min values' do +# it 'returns nil if the list is empty' do +# # Act-Assert +# expect(@list.find_max()).must_be_nil +# expect(@list.find_min()).must_be_nil +# end + +# it 'can retrieve the max and min values in the list' do +# # Arrange +# count = 0 +# while count < 5 +# @list.add_first(count) +# expect(@list.find_max).must_equal count +# expect(@list.find_min).must_equal 0 +# count += 1 +# end +# @list.add_last(100) +# @list.add_first(-12) - # Act-Assert - expect(@list.find_max).must_equal 100 - expect(@list.find_min).must_equal(-12) - end - end - - describe "delete" do - it "delete from empty linked list is a no-op" do - # Assert - expect(@list.length).must_equal 0 - # Act - @list.delete(4) - # Assert - expect(@list.length).must_equal 0 - end - - it "can delete valid values from list" do - # Arrange - @list.add_last(9) - @list.add_last(10) - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) +# # Act-Assert +# expect(@list.find_max).must_equal 100 +# expect(@list.find_min).must_equal(-12) +# end +# end + +# describe "delete" do +# it "delete from empty linked list is a no-op" do +# # Assert +# expect(@list.length).must_equal 0 +# # Act +# @list.delete(4) +# # Assert +# expect(@list.length).must_equal 0 +# end + +# it "can delete valid values from list" do +# # Arrange +# @list.add_last(9) +# @list.add_last(10) +# @list.add_first(4) +# @list.add_first(3) +# @list.add_first(2) - # Act - # delete fist node (requires updating head) - @list.delete(2) +# # Act +# # delete fist node (requires updating head) +# @list.delete(2) - # Assert - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 4 - expect(@list.get_last).must_equal 10 - expect(@list.find_max).must_equal 10 - expect(@list.find_min).must_equal 3 +# # Assert +# expect(@list.get_first).must_equal 3 +# expect(@list.length).must_equal 4 +# expect(@list.get_last).must_equal 10 +# expect(@list.find_max).must_equal 10 +# expect(@list.find_min).must_equal 3 - # Act (again) - # delete last node - @list.delete(10) - # Assert - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 3 - expect(@list.get_last).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - - # delete fist node (requires updating head) - @list.delete(4) - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 2 - expect(@list.get_last).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - end - end - - describe "nth_from_the_end" do - it 'returns nil if n is outside the bounds of the list' do - # Act-Assert - expect(@list.find_nth_from_end(3)).must_be_nil - end - - it 'can retrieve an item at index n from the end in the list' do - # Arrange - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - # Act-Assert - expect(@list.find_nth_from_end(0)).must_equal 1 - expect(@list.find_nth_from_end(1)).must_equal 2 - expect(@list.find_nth_from_end(2)).must_equal 3 - expect(@list.find_nth_from_end(3)).must_equal 4 - expect(@list.find_nth_from_end(4)).must_be_nil - end - end - - describe "reverse" do - it 'can retrieve an item at index n from the end in the list' do - # Arrange - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - @list.add_first(1) +# # Act (again) +# # delete last node +# @list.delete(10) +# # Assert +# expect(@list.get_first).must_equal 3 +# expect(@list.length).must_equal 3 +# expect(@list.get_last).must_equal 9 +# expect(@list.find_max).must_equal 9 +# expect(@list.find_min).must_equal 3 + +# # delete fist node (requires updating head) +# @list.delete(4) +# expect(@list.get_first).must_equal 3 +# expect(@list.length).must_equal 2 +# expect(@list.get_last).must_equal 9 +# expect(@list.find_max).must_equal 9 +# expect(@list.find_min).must_equal 3 +# end +# end + +# describe "nth_from_the_end" do +# it 'returns nil if n is outside the bounds of the list' do +# # Act-Assert +# expect(@list.find_nth_from_end(3)).must_be_nil +# end + +# it 'can retrieve an item at index n from the end in the list' do +# # Arrange +# @list.add_first(1) +# @list.add_first(2) +# @list.add_first(3) +# @list.add_first(4) + +# # Act-Assert +# expect(@list.find_nth_from_end(0)).must_equal 1 +# expect(@list.find_nth_from_end(1)).must_equal 2 +# expect(@list.find_nth_from_end(2)).must_equal 3 +# expect(@list.find_nth_from_end(3)).must_equal 4 +# expect(@list.find_nth_from_end(4)).must_be_nil +# end +# end + +# describe "reverse" do +# it 'can retrieve an item at index n from the end in the list' do +# # Arrange +# @list.add_first(4) +# @list.add_first(3) +# @list.add_first(2) +# @list.add_first(1) - # Act - @list.reverse - - # Assert - expect(@list.find_nth_from_end(0)).must_equal 1 - expect(@list.find_nth_from_end(1)).must_equal 2 - expect(@list.find_nth_from_end(2)).must_equal 3 - expect(@list.find_nth_from_end(3)).must_equal 4 - end - end +# # Act +# @list.reverse + +# # Assert +# expect(@list.find_nth_from_end(0)).must_equal 1 +# expect(@list.find_nth_from_end(1)).must_equal 2 +# expect(@list.find_nth_from_end(2)).must_equal 3 +# expect(@list.find_nth_from_end(3)).must_equal 4 +# end +# end end From 7ac042e40c333356b454dd744ed999e01e8db8e8 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Fri, 23 Aug 2019 16:57:39 -0700 Subject: [PATCH 02/18] implements length method, tests green --- lib/linked_list.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 2c77dabd..86d7d5cf 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,8 +18,8 @@ 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(n) where n is the number of nodes def add_first(value) if !@head @head = Node.new(value) @@ -59,7 +59,13 @@ def find_min # Time Complexity: # Space Complexity def length - raise NotImplementedError + count = 0 + curr = @head + while curr && @head + curr = curr.next + count += 1 + end + return count end # method that returns the value at a given index in the linked list From 0f7843e10c38a9a2c1536eb97a5d40fcfbda14f0 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Fri, 23 Aug 2019 17:07:41 -0700 Subject: [PATCH 03/18] get_last & add_last w/ green test --- lib/linked_list.rb | 22 +++++++++++++-- test/linked_list_test.rb | 58 ++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 86d7d5cf..4cfcbf53 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -140,7 +140,23 @@ def get_first # Time Complexity: # Space Complexity def add_last(value) - raise NotImplementedError + last = get_last_node() + new_node = Node.new(value) + if last + last.next = new_node + else + @head = new_node + end + return true + end + + def get_last_node + return if !@head + curr = @head + while curr.next + curr = curr.next + end + return curr end # method that returns the value of the last node in the linked list @@ -148,7 +164,9 @@ def add_last(value) # Time Complexity: # Space Complexity def get_last - raise NotImplementedError + last_node = get_last_node() + return last_node.data if last_node + return end # method to insert a new node with specific data value, assuming the linked diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 656b1087..fef1b9e2 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -69,39 +69,39 @@ end end -# describe "addLast & getLast" do -# it "will add to the front if the list is empty" do -# # Arrange -# @list.add_last(1) -# # Act-Assert -# expect(@list.get_first).must_equal 1 -# end + describe "addLast & getLast" do + it "will add to the front if the list is empty" do + # Arrange + @list.add_last(1) + # Act-Assert + expect(@list.get_first).must_equal 1 + end -# it "will put new items to the rear of the list" do -# # Arrange -# @list.add_last(2) -# # Act-Assert -# expect(@list.length).must_equal 1 -# expect(@list.get_last).must_equal 2 + it "will put new items to the rear of the list" do + # Arrange + @list.add_last(2) + # Act-Assert + expect(@list.length).must_equal 1 + expect(@list.get_last).must_equal 2 -# # Arrange -# @list.add_last(3) -# # Act-Assert -# expect(@list.get_first).must_equal 2 -# expect(@list.get_last).must_equal 3 -# expect(@list.length).must_equal 2 + # Arrange + @list.add_last(3) + # Act-Assert + expect(@list.get_first).must_equal 2 + expect(@list.get_last).must_equal 3 + expect(@list.length).must_equal 2 -# @list.add_last(4) -# expect(@list.get_first).must_equal 2 -# expect(@list.get_last).must_equal 4 -# expect(@list.length).must_equal 3 -# end + @list.add_last(4) + expect(@list.get_first).must_equal 2 + expect(@list.get_last).must_equal 4 + expect(@list.length).must_equal 3 + end -# it "will return `nil` for `get_last` if the list is empty" do -# # Act-Assert -# expect(@list.get_last).must_be_nil -# end -# end + it "will return `nil` for `get_last` if the list is empty" do + # Act-Assert + expect(@list.get_last).must_be_nil + end + end # describe 'get_at_index' do # it 'returns nil if the index is outside the bounds of the list' do From 30477a7ed60f40774ec3f1a32997b0d8ffabb8e4 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Fri, 23 Aug 2019 17:13:52 -0700 Subject: [PATCH 04/18] get_at_index implemented, test passing --- lib/linked_list.rb | 10 +++++++++- test/linked_list_test.rb | 36 ++++++++++++++++++------------------ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 4cfcbf53..8f2a59fc 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -74,7 +74,15 @@ def length # Time Complexity: # Space Complexity def get_at_index(index) - raise NotImplementedError + length = length() + return nil if length - 1 < index || index < 0 + curr = @head + count = 0 + while index != count + curr = curr.next + count += 1 + end + return curr.data end # method to print all the values in the linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index fef1b9e2..9d17d589 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -103,26 +103,26 @@ end end -# describe 'get_at_index' do -# it 'returns nil if the index is outside the bounds of the list' do -# # Act-Assert -# expect(@list.get_at_index(3)).must_be_nil -# end + describe 'get_at_index' do + it 'returns nil if the index is outside the bounds of the list' do + # Act-Assert + expect(@list.get_at_index(3)).must_be_nil + end -# it 'can retrieve an item at an index in the list' do -# # Arrange -# @list.add_first(1) -# @list.add_first(2) -# @list.add_first(3) -# @list.add_first(4) + it 'can retrieve an item at an index in the list' do + # Arrange + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) -# # Act-Assert -# expect(@list.get_at_index(0)).must_equal 4 -# expect(@list.get_at_index(1)).must_equal 3 -# expect(@list.get_at_index(2)).must_equal 2 -# expect(@list.get_at_index(3)).must_equal 1 -# end -# end + # Act-Assert + expect(@list.get_at_index(0)).must_equal 4 + expect(@list.get_at_index(1)).must_equal 3 + expect(@list.get_at_index(2)).must_equal 2 + expect(@list.get_at_index(3)).must_equal 1 + end + end # describe 'max and min values' do # it 'returns nil if the list is empty' do From 26428a48a0b81b085bec08b1de76cf887d098cb1 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 25 Aug 2019 12:18:06 -0700 Subject: [PATCH 05/18] min & max implemented, tests green --- lib/linked_list.rb | 22 ++++++++++++++++++-- test/linked_list_test.rb | 44 ++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8f2a59fc..fbe6ad54 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -43,7 +43,16 @@ def search(value) # Time Complexity: # Space Complexity def find_max - raise NotImplementedError + return if !@head + curr = @head + max = curr.data + while curr + if max < curr.data + max = curr.data + end + curr = curr.next + end + return max end # method to return the min value in the linked list @@ -51,7 +60,16 @@ def find_max # Time Complexity: # Space Complexity def find_min - raise NotImplementedError + return if !@head + curr = @head + min = curr.data + while curr + if min > curr.data + min = curr.data + end + curr = curr.next + end + return min end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 9d17d589..467f446c 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -124,30 +124,30 @@ end end -# describe 'max and min values' do -# it 'returns nil if the list is empty' do -# # Act-Assert -# expect(@list.find_max()).must_be_nil -# expect(@list.find_min()).must_be_nil -# end + describe 'max and min values' do + it 'returns nil if the list is empty' do + # Act-Assert + expect(@list.find_max()).must_be_nil + expect(@list.find_min()).must_be_nil + end -# it 'can retrieve the max and min values in the list' do -# # Arrange -# count = 0 -# while count < 5 -# @list.add_first(count) -# expect(@list.find_max).must_equal count -# expect(@list.find_min).must_equal 0 -# count += 1 -# end -# @list.add_last(100) -# @list.add_first(-12) + it 'can retrieve the max and min values in the list' do + # Arrange + count = 0 + while count < 5 + @list.add_first(count) + expect(@list.find_max).must_equal count + expect(@list.find_min).must_equal 0 + count += 1 + end + @list.add_last(100) + @list.add_first(-12) -# # Act-Assert -# expect(@list.find_max).must_equal 100 -# expect(@list.find_min).must_equal(-12) -# end -# end + # Act-Assert + expect(@list.find_max).must_equal 100 + expect(@list.find_min).must_equal(-12) + end + end # describe "delete" do # it "delete from empty linked list is a no-op" do From 33b30cdb00e81664db85b1e94822bed0c9e3006f Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 25 Aug 2019 12:23:18 -0700 Subject: [PATCH 06/18] time/ sapce complexity analysis for implemented methods --- lib/linked_list.rb | 32 +++++++-------- test/linked_list_test.rb | 88 ++++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index fbe6ad54..e73f468e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -40,8 +40,8 @@ def search(value) # method to return the max value in the linked list # returns the data value and not the node - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) where n is the number of nodes def find_max return if !@head curr = @head @@ -57,8 +57,8 @@ 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: O(n) where n is the number of nodes + # Space Complexity: O(n) where n is the number of nodes def find_min return if !@head curr = @head @@ -74,8 +74,8 @@ def find_min # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) where n is the number of nodes def length count = 0 curr = @head @@ -89,8 +89,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: O(n) where n is the number of nodes + # Space Complexity: O(n) where n is the number of nodes def get_at_index(index) length = length() return nil if length - 1 < index || index < 0 @@ -111,8 +111,8 @@ def visit end # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) where n is the number of nodes def delete(value) raise NotImplementedError end @@ -155,16 +155,16 @@ def has_cycle # 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(n) where n is the number of nodes def get_first return @head.data if @head return end # method that inserts a given value as a new last node in the linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) where n is the number of nodes def add_last(value) last = get_last_node() new_node = Node.new(value) @@ -187,8 +187,8 @@ def get_last_node # 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 + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) where n is the number of nodes def get_last last_node = get_last_node() return last_node.data if last_node diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 467f446c..c9d4f303 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -149,54 +149,54 @@ end end -# describe "delete" do -# it "delete from empty linked list is a no-op" do -# # Assert -# expect(@list.length).must_equal 0 -# # Act -# @list.delete(4) -# # Assert -# expect(@list.length).must_equal 0 -# end + describe "delete" do + it "delete from empty linked list is a no-op" do + # Assert + expect(@list.length).must_equal 0 + # Act + @list.delete(4) + # Assert + expect(@list.length).must_equal 0 + end -# it "can delete valid values from list" do -# # Arrange -# @list.add_last(9) -# @list.add_last(10) -# @list.add_first(4) -# @list.add_first(3) -# @list.add_first(2) + it "can delete valid values from list" do + # Arrange + @list.add_last(9) + @list.add_last(10) + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) -# # Act -# # delete fist node (requires updating head) -# @list.delete(2) + # Act + # delete fist node (requires updating head) + @list.delete(2) -# # Assert -# expect(@list.get_first).must_equal 3 -# expect(@list.length).must_equal 4 -# expect(@list.get_last).must_equal 10 -# expect(@list.find_max).must_equal 10 -# expect(@list.find_min).must_equal 3 + # Assert + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 4 + expect(@list.get_last).must_equal 10 + expect(@list.find_max).must_equal 10 + expect(@list.find_min).must_equal 3 -# # Act (again) -# # delete last node -# @list.delete(10) -# # Assert -# expect(@list.get_first).must_equal 3 -# expect(@list.length).must_equal 3 -# expect(@list.get_last).must_equal 9 -# expect(@list.find_max).must_equal 9 -# expect(@list.find_min).must_equal 3 - -# # delete fist node (requires updating head) -# @list.delete(4) -# expect(@list.get_first).must_equal 3 -# expect(@list.length).must_equal 2 -# expect(@list.get_last).must_equal 9 -# expect(@list.find_max).must_equal 9 -# expect(@list.find_min).must_equal 3 -# end -# end + # Act (again) + # delete last node + @list.delete(10) + # Assert + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 3 + expect(@list.get_last).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + + # delete fist node (requires updating head) + @list.delete(4) + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 2 + expect(@list.get_last).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + end + end # describe "nth_from_the_end" do # it 'returns nil if n is outside the bounds of the list' do From 9cec077dcf60b827ec4e110a3a4f0d1e1893c301 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 25 Aug 2019 13:02:35 -0700 Subject: [PATCH 07/18] implemented delete method, tests green --- lib/linked_list.rb | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index e73f468e..c33b0d2d 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -114,7 +114,32 @@ def visit # Time Complexity: O(n) where n is the number of nodes # Space Complexity: O(n) where n is the number of nodes def delete(value) - raise NotImplementedError + return if !@head + curr = @head + if curr.data == value + delete_first_node() + else + while curr.next + if curr.next.data == value + remove_next_node(curr) + else + curr = curr.next + end + end + end + end + + + def delete_first_node + temp = @head + @head = @head.next + temp.next = nil + end + + def remove_next_node(node) + temp = node.next + node.next = node.next.next + temp.next = nil end # method to reverse the singly linked list From 707672a5674a7489e546f5c1bba55208d97ab31d Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 25 Aug 2019 15:25:15 -0700 Subject: [PATCH 08/18] added implementation for nth_from_end, tests green to go --- lib/linked_list.rb | 15 +++++++++++---- test/linked_list_test.rb | 38 +++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index c33b0d2d..b5a4dbd1 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -92,15 +92,21 @@ def length # Time Complexity: O(n) where n is the number of nodes # Space Complexity: O(n) where n is the number of nodes def get_at_index(index) + target_node = node_at_index(index) + return if !target_node + return target_node.data + end + + def node_at_index(index) length = length() - return nil if length - 1 < index || index < 0 + return if length - 1 < index || index < 0 curr = @head count = 0 while index != count curr = curr.next count += 1 end - return curr.data + return curr end # method to print all the values in the linked list @@ -129,7 +135,6 @@ def delete(value) end end - def delete_first_node temp = @head @head = @head.next @@ -164,7 +169,9 @@ def find_middle_value # Time Complexity: # Space Complexity def find_nth_from_end(n) - raise NotImplementedError + target_node = node_at_index(length() - n - 1) + return if !target_node + return target_node.data end # checks if the linked list has a cycle. A cycle exists if any node in the diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index c9d4f303..006ab444 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -198,27 +198,27 @@ end end -# describe "nth_from_the_end" do -# it 'returns nil if n is outside the bounds of the list' do -# # Act-Assert -# expect(@list.find_nth_from_end(3)).must_be_nil -# end + describe "nth_from_the_end" do + it 'returns nil if n is outside the bounds of the list' do + # Act-Assert + expect(@list.find_nth_from_end(3)).must_be_nil + end -# it 'can retrieve an item at index n from the end in the list' do -# # Arrange -# @list.add_first(1) -# @list.add_first(2) -# @list.add_first(3) -# @list.add_first(4) + it 'can retrieve an item at index n from the end in the list' do + # Arrange + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) -# # Act-Assert -# expect(@list.find_nth_from_end(0)).must_equal 1 -# expect(@list.find_nth_from_end(1)).must_equal 2 -# expect(@list.find_nth_from_end(2)).must_equal 3 -# expect(@list.find_nth_from_end(3)).must_equal 4 -# expect(@list.find_nth_from_end(4)).must_be_nil -# end -# end + # Act-Assert + expect(@list.find_nth_from_end(0)).must_equal 1 + expect(@list.find_nth_from_end(1)).must_equal 2 + expect(@list.find_nth_from_end(2)).must_equal 3 + expect(@list.find_nth_from_end(3)).must_equal 4 + expect(@list.find_nth_from_end(4)).must_be_nil + end + end # describe "reverse" do # it 'can retrieve an item at index n from the end in the list' do From 744fb22d369de79a256666f5c3a8c8463e65dc8d Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 25 Aug 2019 17:02:39 -0700 Subject: [PATCH 09/18] implemented reverse, wrote unit test for search and implemented method --- lib/linked_list.rb | 19 +++++++++++-- test/linked_list_test.rb | 60 ++++++++++++++++++++++++++++------------ 2 files changed, 60 insertions(+), 19 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index b5a4dbd1..7eea0204 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -35,7 +35,12 @@ def add_first(value) # Time Complexity: # Space Complexity def search(value) - raise NotImplementedError + curr = @head + while curr + return true if curr.data == value + curr = curr.next + end + return false end # method to return the max value in the linked list @@ -152,9 +157,19 @@ def remove_next_node(node) # Time Complexity: # Space Complexity def reverse - raise NotImplementedError + curr = @head + reverse_links(curr) end + def reverse_links(curr) + if !curr.next + @head = curr + return + end + reverse_links(curr.next) + curr.next.next = curr + curr.next = nil + end ## Advanced Exercises # returns the value at the middle element in the singly linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 006ab444..68631721 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -220,22 +220,48 @@ end end -# describe "reverse" do -# it 'can retrieve an item at index n from the end in the list' do -# # Arrange -# @list.add_first(4) -# @list.add_first(3) -# @list.add_first(2) -# @list.add_first(1) + describe "reverse" do + it 'can retrieve an item at index n from the end in the list' do + # Arrange + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + + # Act + @list.reverse + + # Assert + expect(@list.find_nth_from_end(0)).must_equal 1 + expect(@list.find_nth_from_end(1)).must_equal 2 + expect(@list.find_nth_from_end(2)).must_equal 3 + expect(@list.find_nth_from_end(3)).must_equal 4 + end + end + + describe "search" do + it "will return false if list is empty" do + expect(@list.search(20)).must_equal false + end + + it "will return false if value is not in list" do + + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) -# # Act -# @list.reverse - -# # Assert -# expect(@list.find_nth_from_end(0)).must_equal 1 -# expect(@list.find_nth_from_end(1)).must_equal 2 -# expect(@list.find_nth_from_end(2)).must_equal 3 -# expect(@list.find_nth_from_end(3)).must_equal 4 -# end -# end + expect(@list.search(20)).must_equal false + end + + it "will return true if value is found" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + + expect(@list.search(2)).must_equal true + end + + end end From 25a932c52a6ede525af80a48ae3e96f31e9d891b Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 25 Aug 2019 17:10:37 -0700 Subject: [PATCH 10/18] implemented modified visit method and unit tests --- lib/linked_list.rb | 14 ++++++++++---- test/linked_list_test.rb | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 7eea0204..6d5af63a 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -114,11 +114,17 @@ def node_at_index(index) return curr end - # method to print all the values in the linked list - # Time Complexity: - # Space Complexity + # method to return a string of all the values in the linked list + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(n) where n is the number of nodes def visit - raise NotImplementedError + node_values = "" + curr = @head + while curr + node_values << curr.data.to_s + curr = curr.next + end + return node_values end # method to delete the first node found with specified value diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 68631721..72572bda 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -261,7 +261,24 @@ @list.add_first(1) expect(@list.search(2)).must_equal true + expect(@list.search(3)).must_equal true + expect(@list.search(1)).must_equal true + expect(@list.search(4)).must_equal true + end + end + + describe "visit" do + it "will return empty string if list is empty" do + expect(@list.visit).must_equal "" end + it "will return all the values in order as a string" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + expect(@list.visit).must_equal "1234" + + end end end From 4effb5e1f8b9899b3a24d44c1e1d196a0589f599 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Sun, 25 Aug 2019 17:23:05 -0700 Subject: [PATCH 11/18] implemented find_middle_value with unit tests --- lib/linked_list.rb | 8 +++++++- test/linked_list_test.rb | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 6d5af63a..d31a8428 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -182,7 +182,13 @@ def reverse_links(curr) # Time Complexity: # Space Complexity def find_middle_value - raise NotImplementedError + return if !@head + fast,slow = @head, @head + while fast.next && fast.next.next + fast = fast.next.next + slow = slow.next + end + return slow.data end # find the nth node from the end and return its value diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 72572bda..e88c0a3a 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -281,4 +281,28 @@ end end + + describe "find_middle_value" do + it "will return nil if list is empty" do + expect(@list.find_middle_value).must_be_nil + end + + it "will return the middle value if list length is odd" do + @list.add_first(5) + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + expect(@list.find_middle_value).must_equal 3 + + end + + it "will return the first value left of center if no middle value (ie list length is even)" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + expect(@list.find_middle_value).must_equal 2 + end + end end From 5989312110e9fe550d47bbe192f768eb372c2ff6 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 26 Aug 2019 08:09:07 -0700 Subject: [PATCH 12/18] cycle method implemented with unit tests --- lib/linked_list.rb | 11 ++++++++++- test/linked_list_test.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index d31a8428..06f712df 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -207,7 +207,16 @@ def find_nth_from_end(n) # Time Complexity: # Space Complexity def has_cycle - raise NotImplementedError + return false if !@head + fast, slow = @head, @head + while fast.next.next + fast = fast.next.next + slow = slow.next + if fast == slow + return true + end + end + return false end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index e88c0a3a..75c61eb5 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -305,4 +305,33 @@ expect(@list.find_middle_value).must_equal 2 end end + + describe "has_cycle" do + it "will return false if list is empty" do + expect(@list.has_cycle).must_equal false + end + + it "will return true if list has a cyle" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + @list.create_cycle() + expect(@list.has_cycle).must_equal true + end + + it "will return true if list is length one and has a cycle" do + @list.add_first(4) + @list.create_cycle() + expect(@list.has_cycle).must_equal true + end + it "will return false if list does not have a cycle" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + expect(@list.has_cycle).must_equal false + end + end + end From 937c236bc13c29050e50511bb04bc044bca57f8d Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 26 Aug 2019 08:30:22 -0700 Subject: [PATCH 13/18] added tests and implementation for insert_ascending(value) --- lib/linked_list.rb | 34 +++++++++++++++++++++++----------- test/linked_list_test.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 11 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 06f712df..b1e94dc0 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -160,8 +160,8 @@ def remove_next_node(node) # 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: passing pointer not list to rec statck so O(n) def reverse curr = @head reverse_links(curr) @@ -179,8 +179,8 @@ def reverse_links(curr) ## Advanced Exercises # returns the value at the middle element in the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O(n) where n is the number of nodes def find_middle_value return if !@head fast,slow = @head, @head @@ -193,8 +193,8 @@ def find_middle_value # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O(n) where n is the number of nodes def find_nth_from_end(n) target_node = node_at_index(length() - n - 1) return if !target_node @@ -204,8 +204,8 @@ def find_nth_from_end(n) # 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 + # Time Complexity: O(n) + # Space Complexity: O(n) where n is the number of nodes def has_cycle return false if !@head fast, slow = @head, @head @@ -265,10 +265,22 @@ def get_last # method to insert a new node with specific data value, assuming the linked # list is sorted in ascending order - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O(n) where n is the number of nodes def insert_ascending(value) - raise NotImplementedError + new_node = Node.new(value) + if !@head || value <= @head.data + new_node.next = @head + @head = new_node + return + end + curr = @head + while curr.next && curr.next.data <= value + curr = curr.next + end + temp = curr.next + curr.next = new_node + new_node.next = temp end # Helper method for tests diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 75c61eb5..ed3e67eb 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -334,4 +334,35 @@ end end + + describe "insert_ascending(value)" do + it "will add to list if list is empty" do + @list.insert_ascending(5) + expect(@list.length).must_equal 1 + expect(@list.get_first()).must_equal 5 + end + + it 'will add to front of list if less than all values' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.insert_ascending(1) + expect(@list.length).must_equal 4 + expect(@list.get_first()).must_equal 1 + + end + + it "will add to back of list if greater than all values" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.insert_ascending(5) + expect(@list.length).must_equal 4 + expect(@list.get_last()).must_equal 5 + end + + it "will add in middle in correct order" do + end + end + end From 1663e188833af845ead68d5574c386f293653724 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 26 Aug 2019 17:56:57 -0700 Subject: [PATCH 14/18] updated space complex analysis to not include object link list --- lib/linked_list.rb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index b1e94dc0..44089742 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,7 +19,7 @@ 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: O(1) - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def add_first(value) if !@head @head = Node.new(value) @@ -46,7 +46,7 @@ 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) where n is the number of nodes - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def find_max return if !@head curr = @head @@ -63,7 +63,7 @@ def find_max # method to return the min value in the linked list # returns the data value and not the node # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def find_min return if !@head curr = @head @@ -80,7 +80,7 @@ def find_min # method that returns the length of the singly linked list # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def length count = 0 curr = @head @@ -95,7 +95,7 @@ def length # index count starts at 0 # returns nil if there are fewer nodes in the linked list than the index value # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def get_at_index(index) target_node = node_at_index(index) return if !target_node @@ -116,7 +116,7 @@ def node_at_index(index) # method to return a string of all the values in the linked list # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def visit node_values = "" curr = @head @@ -129,7 +129,7 @@ def visit # method to delete the first node found with specified value # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def delete(value) return if !@head curr = @head @@ -180,7 +180,7 @@ def reverse_links(curr) ## Advanced Exercises # returns the value at the middle element in the singly linked list # Time Complexity: O(n) - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def find_middle_value return if !@head fast,slow = @head, @head @@ -194,7 +194,7 @@ def find_middle_value # find the nth node from the end and return its value # assume indexing starts at 0 while counting to n # Time Complexity: O(n) - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def find_nth_from_end(n) target_node = node_at_index(length() - n - 1) return if !target_node @@ -205,7 +205,7 @@ def find_nth_from_end(n) # linked list links to a node already visited. # returns true if a cycle is found, false otherwise. # Time Complexity: O(n) - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def has_cycle return false if !@head fast, slow = @head, @head @@ -224,7 +224,7 @@ def has_cycle # returns the value in the first node # returns nil if the list is empty # Time Complexity: O(1) - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def get_first return @head.data if @head return @@ -232,7 +232,7 @@ def get_first # method that inserts a given value as a new last node in the linked list # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def add_last(value) last = get_last_node() new_node = Node.new(value) @@ -256,7 +256,7 @@ def get_last_node # method that returns the value of the last node in the linked list # returns nil if the linked list is empty # Time Complexity: O(n) where n is the number of nodes - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def get_last last_node = get_last_node() return last_node.data if last_node @@ -266,7 +266,7 @@ def get_last # method to insert a new node with specific data value, assuming the linked # list is sorted in ascending order # Time Complexity: O(n) - # Space Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def insert_ascending(value) new_node = Node.new(value) if !@head || value <= @head.data From 8d85edcbf19e20657e7c6ed1a8c486262fe662a2 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 26 Aug 2019 17:58:08 -0700 Subject: [PATCH 15/18] adds missing space/ time analysis for search method --- 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 44089742..8eff826e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -32,8 +32,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: O(n) where n is the number of nodes + # Space Complexity: O(1) def search(value) curr = @head while curr From 43a669c66c193125179c3e30915548e01d7e0c21 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 26 Aug 2019 18:13:16 -0700 Subject: [PATCH 16/18] adds tail pointer and update methods and time complex --- lib/linked_list.rb | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8eff826e..8d0a5eb7 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -14,6 +14,7 @@ def initialize(value, next_node = nil) class LinkedList def initialize @head = nil # keep the head private. Not accessible outside this class + @tail = nil end # method to add a new node with the specific data value in the linked list @@ -21,8 +22,10 @@ def initialize # Time Complexity: O(1) # Space Complexity: O(1) def add_first(value) + new_node = Node.new(value) if !@head - @head = Node.new(value) + @head = new_node + @tail = new_node else new_node = Node.new(value) new_node.next = @head @@ -156,6 +159,7 @@ def remove_next_node(node) temp = node.next node.next = node.next.next temp.next = nil + @tail = node if !node.next end # method to reverse the singly linked list @@ -234,33 +238,25 @@ def get_first # Time Complexity: O(n) where n is the number of nodes # Space Complexity: O(1) def add_last(value) - last = get_last_node() new_node = Node.new(value) - if last - last.next = new_node + if @tail + @tail.next = new_node + @tail = new_node else @head = new_node + @tail = new_node end return true end - def get_last_node - return if !@head - curr = @head - while curr.next - curr = curr.next - end - return curr - end # method that returns the value of the last node in the linked list # returns nil if the linked list is empty # Time Complexity: O(n) where n is the number of nodes # Space Complexity: O(1) def get_last - last_node = get_last_node() - return last_node.data if last_node - return + return if !@tail + return @tail.data end # method to insert a new node with specific data value, assuming the linked @@ -272,6 +268,7 @@ def insert_ascending(value) if !@head || value <= @head.data new_node.next = @head @head = new_node + @tail = new_node return end curr = @head @@ -279,6 +276,7 @@ def insert_ascending(value) curr = curr.next end temp = curr.next + @tail = new_node if !curr.next curr.next = new_node new_node.next = temp end From f7ad98e53a47ca3ca25cf34ee18f50dfa0c3279d Mon Sep 17 00:00:00 2001 From: qqdipps Date: Mon, 26 Aug 2019 18:23:13 -0700 Subject: [PATCH 17/18] adds explicit return of nil when no return is expected as to not leak data, adds @length instance var to ll class --- lib/linked_list.rb | 33 ++++++++++++++++++++++----------- test/linked_list_test.rb | 2 ++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8d0a5eb7..2d847a09 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -15,6 +15,7 @@ class LinkedList def initialize @head = nil # keep the head private. Not accessible outside this class @tail = nil + @length = 0 end # method to add a new node with the specific data value in the linked list @@ -31,6 +32,8 @@ def add_first(value) new_node.next = @head @head = new_node end + @length += 1 + return end # method to find if the linked list contains a node with specified value @@ -82,16 +85,17 @@ def find_min # method that returns the length of the singly linked list - # Time Complexity: O(n) where n is the number of nodes + # Time Complexity: O(1) (was O(n) updated class to keep track on length) # Space Complexity: O(1) def length - count = 0 - curr = @head - while curr && @head - curr = curr.next - count += 1 - end - return count + # count = 0 + # curr = @head + # while curr && @head + # curr = curr.next + # count += 1 + # end + # return count + return @length end # method that returns the value at a given index in the linked list @@ -147,6 +151,8 @@ def delete(value) end end end + @length -= 1 + return end def delete_first_node @@ -179,6 +185,7 @@ def reverse_links(curr) reverse_links(curr.next) curr.next.next = curr curr.next = nil + return end ## Advanced Exercises @@ -235,7 +242,7 @@ def get_first end # method that inserts a given value as a new last node in the linked list - # Time Complexity: O(n) where n is the number of nodes + # Time Complexity: O(1) # Space Complexity: O(1) def add_last(value) new_node = Node.new(value) @@ -246,13 +253,14 @@ def add_last(value) @head = new_node @tail = new_node end - return true + @length += 1 + return end # method that returns the value of the last node in the linked list # returns nil if the linked list is empty - # Time Complexity: O(n) where n is the number of nodes + # Time Complexity: O(1) # Space Complexity: O(1) def get_last return if !@tail @@ -269,6 +277,7 @@ def insert_ascending(value) new_node.next = @head @head = new_node @tail = new_node + @length += 1 return end curr = @head @@ -279,6 +288,8 @@ def insert_ascending(value) @tail = new_node if !curr.next curr.next = new_node new_node.next = temp + @length += 1 + return end # Helper method for tests diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index ed3e67eb..5bb81d53 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -337,6 +337,7 @@ describe "insert_ascending(value)" do it "will add to list if list is empty" do + expect(@list.length).must_equal 0 @list.insert_ascending(5) expect(@list.length).must_equal 1 expect(@list.get_first()).must_equal 5 @@ -346,6 +347,7 @@ @list.add_first(4) @list.add_first(3) @list.add_first(2) + expect(@list.length).must_equal 3 @list.insert_ascending(1) expect(@list.length).must_equal 4 expect(@list.get_first()).must_equal 1 From 92f9dfb65581287a2ad0891c432785275f576891 Mon Sep 17 00:00:00 2001 From: qqdipps Date: Wed, 28 Aug 2019 01:03:01 -0700 Subject: [PATCH 18/18] edge and tail to reverse ' --- lib/linked_list.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 2d847a09..54f27282 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -173,7 +173,9 @@ def remove_next_node(node) # Time Complexity: O(n) # Space Complexity: passing pointer not list to rec statck so O(n) def reverse + return if !@head curr = @head + @tail = curr reverse_links(curr) end