From cbd0368658c6f52b669b106df7f92608665baef3 Mon Sep 17 00:00:00 2001 From: Ida Date: Thu, 8 Apr 2021 20:06:44 -0700 Subject: [PATCH 1/3] Required tests are passing --- lib/linked_list.rb | 82 +++++++++++++++++++++++++++++++++++----- test/linked_list_test.rb | 8 ++-- 2 files changed, 77 insertions(+), 13 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..94860793 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 + node = Node.new(value, @head) #head is nil + @head = node end # method to find if the linked list contains a node with specified value @@ -29,13 +30,34 @@ def add_first(value) # Time Complexity: ? # Space Complexity: ? def search(value) - raise NotImplementedError + current_node = @head + + while current_node + if current_node.data == value + return true + else + current_node = current_node.next + 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 + current_node = @head + return if @head.nil? + max = current_node.data + + while current_node.next + if current_node.next.data > max + max = current_node.next.data + end + current_node = current_node.next + end + + return max end # method to return the min value in the linked list @@ -43,7 +65,18 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min - raise NotImplementedError + current_node = @head + return if @head.nil? + min = current_node.data + + while current_node.next + if current_node.next.data < min + min = current_node.next.data + end + current_node = current_node.next + end + + return min end @@ -53,21 +86,37 @@ def find_min # Time Complexity: ? # Space Complexity: ? def get_first - raise NotImplementedError + @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 add_first(value) if @head.nil? #if list is empty, adding to the first of the list is the same as adding to the end of the list + + node = @head + while !node.next.nil? #when next is nil, we are at the end of the list + next_node = node.next + node = next_node + end + + node.next = Node.new(value) end # method that returns the length of the singly linked list # Time Complexity: ? # Space Complexity: ? def length - raise NotImplementedError + count = 0 + node = @head + + while !node.nil? + count += 1 + next_node = node.next + node = next_node + end + count end # method that returns the value at a given index in the linked list @@ -76,7 +125,15 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - raise NotImplementedError + count = 0 + node = @head + while count < index + return if node.nil? + node = node.next + count += 1 + end + + node.data end # method to print all the values in the linked list @@ -106,7 +163,14 @@ def reverse # Time Complexity: ? # Space Complexity: ? def get_last - raise NotImplementedError + return if @head.nil? + + node = @head #node is the current object + while !node.next.nil? #iterating through objects until node.next is nil + # next_node = node.next + node = node.next + end + node.data end ## Advanced Exercises diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 77423e34..8a152328 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -32,18 +32,18 @@ @list.add_first(2) # Assert - expect(@list.get_at_index(0)).must_equal 2 + expect(@list.get_first).must_equal 2 # Act again @list.add_first(3) # Assert - expect(@list.get_at_index(0)).must_equal 3 + expect(@list.get_first).must_equal 3 end it 'will return `nil` for `getFirst` if the list is empty' do - expect(@list.get_at_index(0)).must_be_nil + expect(@list.get_first).must_be_nil end end @@ -103,7 +103,7 @@ end end - xdescribe "Optional addLast & getLast" do + describe "Optional addLast & getLast" do it "will add to the front if the list is empty" do @list.add_last(1) expect(@list.get_at_index(0)).must_equal 1 From 077a018ded485ac757bcaf379d194d452d302889 Mon Sep 17 00:00:00 2001 From: Ida Date: Thu, 8 Apr 2021 20:10:10 -0700 Subject: [PATCH 2/3] Added time and space complexity --- lib/linked_list.rb | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 94860793..d1fa40bb 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(1) def add_first(value) node = Node.new(value, @head) #head is nil @head = node @@ -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: O(n) + # Space Complexity: O(1) def search(value) current_node = @head @@ -45,6 +45,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) def find_max current_node = @head return if @head.nil? @@ -62,8 +64,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) + # Space Complexity: O(1) def find_min current_node = @head return if @head.nil? @@ -83,15 +85,15 @@ def find_min # 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 @head&.data end # method that inserts a given value as a new last node in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time complexity - O(n) + # Space complexity - O(1) def add_last(value) return add_first(value) if @head.nil? #if list is empty, adding to the first of the list is the same as adding to the end of the list @@ -105,8 +107,8 @@ def add_last(value) 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 count = 0 node = @head @@ -122,8 +124,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) + # Space Complexity: O(1) def get_at_index(index) count = 0 node = @head From 582d06051e9a0fcf84bf1c66e35efe6f792ebc11 Mon Sep 17 00:00:00 2001 From: Ida Date: Thu, 8 Apr 2021 20:38:04 -0700 Subject: [PATCH 3/3] Tests for delete method are passing --- lib/linked_list.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index d1fa40bb..db4c32e2 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -146,10 +146,22 @@ def visit end # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def delete(value) - raise NotImplementedError + return if @head.nil? + current_node = @head + + if current.data == value + @head = current.next + else + while (current.next != nil) && (current.next.data != value) + current = current.next + end + unless current.next == nil + current.next =current.next.next + end + end end # method to reverse the singly linked list