From 1a729e05520009caa5843a910246c74d8384331e Mon Sep 17 00:00:00 2001 From: Maha-ElMais Date: Wed, 24 Mar 2021 00:12:01 -0700 Subject: [PATCH 1/4] add_first, search --- lib/linked_list.rb | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..5832a382 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 + #raise NotImplementedError + @head = Node.new(value, @head) #adding a linked list value 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 + #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 + if @head == nil #return nil if head.nil? + return nil + end + max_val = @head.data + current = @head + until current == nil + if current.data > max_val + max_val = current.data + end + current = current.next + end + return max_val end # method to return the min value in the linked list From 6afe62646957d55f4d4eeb4312b6d5a6c1d7761f Mon Sep 17 00:00:00 2001 From: Maha-ElMais Date: Wed, 24 Mar 2021 00:15:25 -0700 Subject: [PATCH 2/4] find_max, find_min , get_first --- lib/linked_list.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5832a382..fbf83007 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -65,7 +65,19 @@ def find_max # Time Complexity: ? # Space Complexity: ? def find_min - raise NotImplementedError + #raise NotImplementedError + if @head == nil + return nil + end + min_val = @head.data + current = @head + until current == nil + if current.data < min_val + min_val = current.data + end + current = current.next + end + return min_val end @@ -75,7 +87,8 @@ def find_min # Time Complexity: ? # Space Complexity: ? def get_first - raise NotImplementedError + #raise NotImplementedError + return @head.nil? ? nil : @head.data #if this is true do this else do the latter end # method that inserts a given value as a new last node in the linked list From f2419eea54dc68fe93c25f0a22b605e3d3dc738e Mon Sep 17 00:00:00 2001 From: Maha-ElMais Date: Wed, 24 Mar 2021 00:22:11 -0700 Subject: [PATCH 3/4] length, get_at_index, delete, get_last --- lib/linked_list.rb | 58 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index fbf83007..06187214 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -95,14 +95,31 @@ def get_first # Time Complexity: ? # Space Complexity: ? 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) # not current because it is outside of the scope of until + 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 @@ -111,7 +128,18 @@ def length # Time Complexity: ? # Space Complexity: ? def get_at_index(index) - raise NotImplementedError + #raise NotImplementedError + count = 0 + + current = @head + until current == nil + if count == index + return current.data + else + count+= 1 + current = current.next + end + end end # method to print all the values in the linked list @@ -125,7 +153,20 @@ def visit # Time Complexity: ? # Space Complexity: ? def delete(value) - raise NotImplementedError + #raise NotImplementedError + if @head == nil + return nil + end + if @head.data == value + @head = @head.next + end + current = @head + until current.next == nil + if current.next.data == value + current.next = current.next.next + end + current = current.next + end end # method to reverse the singly linked list @@ -141,7 +182,14 @@ 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 From 504fecb6fc2692dbdf12f8c1678d98bbbd8689d9 Mon Sep 17 00:00:00 2001 From: Maha-ElMais Date: Mon, 29 Mar 2021 20:52:59 -0700 Subject: [PATCH 4/4] time space complexity --- lib/linked_list.rb | 88 ++++++++++++++++++++++++---------------- test/linked_list_test.rb | 2 +- 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 06187214..bb25c504 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: O(n) + # Space Complexity: O(1) def search(value) #raise NotImplementedError current = @head @@ -44,6 +44,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 if @head == nil #return nil 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 #raise NotImplementedError if @head == nil @@ -84,16 +86,17 @@ 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 #if this is true do this else do the latter + return @head.nil? ? nil : @head.data + #if head is nil is true? return nil if flase return @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 if @head == nil @@ -108,8 +111,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 @@ -125,8 +128,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 count = 0 @@ -143,44 +146,61 @@ 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 + #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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def delete(value) #raise NotImplementedError - if @head == nil - return nil - end - if @head.data == value + return nil if @head.nil? + + if @head.data == value @head = @head.next - end - current = @head - until current.next == nil - if current.next.data == value - current.next = current.next.next - end - current = current.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 # 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 + #raise NotImplementedError + return nil if @head.nil? + + current = @head + prev = nil + + 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? 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