From 4975919affaf9792156be3a8c48b03fc5eb56b58 Mon Sep 17 00:00:00 2001 From: gessica Date: Tue, 23 Mar 2021 23:21:43 -0700 Subject: [PATCH 1/2] created methods missing reverse method --- .idea/.gitignore | 6 ++ .idea/linked-list.iml | 27 ++++++++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ lib/linked_list.rb | 136 +++++++++++++++++++++++++++++++++------ test/linked_list_test.rb | 2 +- 7 files changed, 170 insertions(+), 19 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/linked-list.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..8bf4d45d --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,6 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/linked-list.iml b/.idea/linked-list.iml new file mode 100644 index 00000000..db98faa2 --- /dev/null +++ b/.idea/linked-list.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..510e7fcc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..a096c061 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..cf3e1f01 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,24 +18,50 @@ 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) - raise NotImplementedError + # raise NotImplementedError + @head = Node.new(value, @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) - raise NotImplementedError + # raise NotImplementedError + current = @head + + until current == nil + if current.data == value + return true + else + current = current.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 + # raise NotImplementedError + return nil if @head.nil? + + max_value = @head.data + current = @head + + until current == nil + if current.data > max_value + max_value = current.data + end + current = current.next + end + + return max_value + end # method to return the min value in the linked list @@ -43,7 +69,21 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min - raise NotImplementedError + # raise NotImplementedError + return nil if @head.nil? + + min_value = @head.data + current = @head + + until current == nil + if current.data < min_value + min_value = current.data + end + current = current.next + end + + return min_value + end @@ -53,21 +93,38 @@ def find_min # Time Complexity: ? # Space Complexity: ? def get_first - raise NotImplementedError + # raise NotImplementedError + return @head.nil? ? nil : @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) - raise NotImplementedError + # raise NotImplementedError + if @head.nil? + @head = Node.new(value) + else + current = @head + until current.next.nil? + current = current.next + end + current.next = Node.new(value) + end end # method that returns the length of the singly linked list # Time Complexity: ? # Space Complexity: ? def length - raise NotImplementedError + # raise NotImplementedError + count = 0 + current = @head + until current.nil? + current = current.next + count += 1 + end + return count end # method that returns the value at a given index in the linked list @@ -76,21 +133,50 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - raise NotImplementedError + # raise NotImplementedError + counter = 0 + current = @head + until current.nil? + if index == counter + return current.data + else + counter += 1 + current = current.next + end + end end # method to print all the values in the linked list # Time Complexity: ? # Space Complexity: ? def visit - raise NotImplementedError + # raise NotImplementedError + return nil if @head.nil? + + current = @head + until current.nil? + puts current.data + end + end # method to delete the first node found with specified value # Time Complexity: ? # Space Complexity: ? def delete(value) - raise NotImplementedError + # raise NotImplementedError + return nil if @head.nil? + + if @head.data == value + @head = @head.next + else + current = @head + until current.next.data == value + current = current.next + end + current.next = current.next.next + end + end # method to reverse the singly linked list @@ -98,7 +184,13 @@ def delete(value) # Time Complexity: ? # Space Complexity: ? def reverse - raise NotImplementedError + # raise NotImplementedError + prev = nil + current = @head + + until current.nil? + temp = + end end # method that returns the value of the last node in the linked list @@ -106,7 +198,15 @@ def reverse # Time Complexity: ? # Space Complexity: ? def get_last - raise NotImplementedError + # raise NotImplementedError + return nil if @head.nil? + + current = @head + until current.next.nil? + current = current.next + end + + return current.data end ## Advanced Exercises diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 77423e34..8524901f 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -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 4d4754d3dd3d162b31f6979b58b0a45a710d849f Mon Sep 17 00:00:00 2001 From: gessica Date: Wed, 24 Mar 2021 20:00:09 -0700 Subject: [PATCH 2/2] implemented reverse method and added time and space complexity to all methods --- lib/linked_list.rb | 47 +++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index cf3e1f01..4b238b0e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -46,6 +46,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 # raise NotImplementedError return nil if @head.nil? @@ -66,8 +68,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 # raise NotImplementedError return nil if @head.nil? @@ -90,8 +92,8 @@ 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 # raise NotImplementedError return @head.nil? ? nil : @head.data @@ -114,8 +116,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 # raise NotImplementedError count = 0 @@ -130,8 +132,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) # raise NotImplementedError counter = 0 @@ -147,8 +149,8 @@ def get_at_index(index) end # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def visit # raise NotImplementedError return nil if @head.nil? @@ -161,8 +163,8 @@ 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 nil if @head.nil? @@ -181,22 +183,29 @@ 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: O(n) + # Space Complexity: O(1) def reverse # raise NotImplementedError - prev = nil + return nil if @head.nil? + current = @head + prev = nil - until current.nil? - temp = + while current + temp = current.next + current.next = prev + prev = current + current = temp end + @head = prev + 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_last # raise NotImplementedError return nil if @head.nil?