From f57631bf874e7411af4bc49e6ad1b32ba90dedcd Mon Sep 17 00:00:00 2001 From: Alice Boone Date: Thu, 25 Mar 2021 14:24:08 -0700 Subject: [PATCH 1/3] linked list exercises solved part 1 --- lib/linked_list.rb | 295 +++++++++++++++++++++++---------------- test/linked_list_test.rb | 6 +- 2 files changed, 179 insertions(+), 122 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..ef1b7c76 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -12,148 +12,205 @@ def initialize(value, next_node = nil) # Defines the singly linked list class LinkedList - def initialize - @head = nil # keep the head private. Not accessible outside this class - end + def initialize + @head = nil # keep the head private. Not accessible outside this class + end - # 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: ? - def add_first(value) - raise NotImplementedError - end + # 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(1) + def add_first(value) + new_node = Node.new(value) + new_node.next = @head + @head = new_node + end - # method to find if the linked list contains a node with specified value - # returns true if found, false otherwise - # Time Complexity: ? - # Space Complexity: ? - def search(value) - raise NotImplementedError - end + # method to find if the linked list contains a node with specified value + # returns true if found, false otherwise + # Time Complexity: O(n) + # Space Complexity: O(1) + def search(value) + current = @head - # method to return the max value in the linked list - # returns the data value and not the node - def find_max - raise NotImplementedError - end + until current.nil? + return true if current.data == value - # method to return the min value in the linked list - # returns the data value and not the node - # Time Complexity: ? - # Space Complexity: ? - def find_min - raise NotImplementedError + current = current.next 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 + end - # Additional Exercises - # returns the value in the first node - # returns nil if the list is empty - # Time Complexity: ? - # Space Complexity: ? - def get_first - raise NotImplementedError - end + # method to return the min value in the linked list + # returns the data value and not the node + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_min + raise NotImplementedError + 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 - end - # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? - def length - raise NotImplementedError - end + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + # Time Complexity: O(1) + # Space Complexity: O(1) + def get_first + return @head.nil? ? @head : @head.data + 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: ? - def get_at_index(index) - raise NotImplementedError + # method that inserts a given value as a new last node in the linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + def add_last(value) + if @head.nil? + @head = Node.new(value) + else + last = Node.new(value) + current =@head + + while current.next + current = current.next + end + current.next = last end + end - # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? - def visit - raise NotImplementedError - end + # method that returns the length of the singly linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + def length + return 0 if @head.nil? - # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? - def delete(value) - raise NotImplementedError - end + count = 0 + current = @head - # 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: ? - def reverse - raise NotImplementedError + while current != nil + count = count + 1 + current = current.next end + return count + 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: ? - def get_last - raise NotImplementedError - end - - ## Advanced Exercises - # returns the value at the middle element in the singly linked list - # Time Complexity: ? - # Space Complexity: ? - def find_middle_value - raise NotImplementedError + # 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: O(n) + # Space Complexity: O(1) + def get_at_index(index) + current = @head + count = 0 + + until current.nil? + if index == count + return current.data + else + current = current.next + count = count + 1 + end end + end - # find the nth node from the end and return its value - # assume indexing starts at 0 while counting to n - # Time Complexity: ? - # Space Complexity: ? - def find_nth_from_end(n) - raise NotImplementedError - end + # method to print all the values in the linked list + # Time Complexity: ? + # Space Complexity: ? + def visit + raise NotImplementedError + end + + # method to delete the first node found with specified value + # Time Complexity: ? + # Space Complexity: ? + def delete(value) + raise NotImplementedError + 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: ? + def reverse + return nil if @head.nil? + + current = @head + previous = nil + next_node = nil - # 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: ? - def has_cycle - raise NotImplementedError + while current + next_node = current.next + current.next = previous + previous = current + current = next_node end - # method to insert a new node with specific data value, assuming the linked - # list is sorted in ascending order - # Time Complexity: ? - # Space Complexity: ? - def insert_ascending(value) - raise NotImplementedError + @head = previous + 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) + # Space Complexity: O(1) + def get_last + return @head if @head.nil? + + current = @head + while current.next + current = current.next end + return current.data + end - # Helper method for tests - # Creates a cycle in the linked list for testing purposes - # Assumes the linked list has at least one node - def create_cycle - return if @head == nil # don't do anything if the linked list is empty + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + # Time Complexity: ? + # Space Complexity: ? + def find_middle_value + raise NotImplementedError + end - # navigate to last node - current = @head - while current.next != nil - current = current.next - end + # find the nth node from the end and return its value + # assume indexing starts at 0 while counting to n + # Time Complexity: ? + # Space Complexity: ? + def find_nth_from_end(n) + raise NotImplementedError + end + + # 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: ? + def has_cycle + raise NotImplementedError + end + + # method to insert a new node with specific data value, assuming the linked + # list is sorted in ascending order + # Time Complexity: ? + # Space Complexity: ? + def insert_ascending(value) + raise NotImplementedError + end + + # Helper method for tests + # Creates a cycle in the linked list for testing purposes + # Assumes the linked list has at least one node + def create_cycle + return if @head == nil # don't do anything if the linked list is empty - current.next = @head # make the last node link to first node + # navigate to last node + current = @head + while current.next != nil + current = current.next end -end + + current.next = @head # make the last node link to first node + end +end \ No newline at end of file diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 77423e34..6ad4cfdb 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 @@ -126,7 +126,7 @@ end end - describe 'max and min values' do + xdescribe 'max and min values' do it 'returns nil if the list is empty' do expect(@list.find_max()).must_be_nil expect(@list.find_min()).must_be_nil @@ -152,7 +152,7 @@ end end - describe "delete" do + xdescribe "delete" do it "delete from empty linked list is a no-op" do expect(@list.length).must_equal 0 @list.delete(4) From d661aad5befa7931cad80debff5014bbf86ab4f8 Mon Sep 17 00:00:00 2001 From: Alice Boone Date: Thu, 25 Mar 2021 16:04:38 -0700 Subject: [PATCH 2/3] linked list exercises solved part 2 --- lib/linked_list.rb | 54 ++++++++++++++++++++++++++++++++++------ test/linked_list_test.rb | 4 +-- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index ef1b7c76..cf003060 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -44,7 +44,16 @@ def search(value) # method to return the max value in the linked list # returns the data value and not the node def find_max - raise NotImplementedError + return nil if @head.nil? + + current = @head + max = current.data + + until current.nil? + max = current.data if current.data > max + current = current.next + end + return max end # method to return the min value in the linked list @@ -52,7 +61,16 @@ def find_max # Time Complexity: O(n) # Space Complexity: O(1) def find_min - raise NotImplementedError + return nil if @head.nil? + + current = @head + min = current.data + + until current.nil? + min = current.data if current.data < min + current = current.next + end + return min end @@ -73,7 +91,7 @@ def add_last(value) @head = Node.new(value) else last = Node.new(value) - current =@head + current = @head while current.next current = current.next @@ -121,14 +139,34 @@ def get_at_index(index) # Time Complexity: ? # Space Complexity: ? def visit - raise NotImplementedError + return @head if @head.nil? + + current = @head + + until current.nil? + p current.data + current = current.next + 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 + return if @head.nil? + + if @head.data == value + @head = @head.next + else + current = @head + until current.next == nil + if current.next.data == value + temp = current.next + current.next = temp.next + end + current = current.next + end + end end # method to reverse the singly linked list @@ -140,7 +178,6 @@ def reverse current = @head previous = nil - next_node = nil while current next_node = current.next @@ -189,6 +226,7 @@ def find_nth_from_end(n) # Space Complexity: ? def has_cycle raise NotImplementedError + 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 6ad4cfdb..8524901f 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -126,7 +126,7 @@ end end - xdescribe 'max and min values' do + describe 'max and min values' do it 'returns nil if the list is empty' do expect(@list.find_max()).must_be_nil expect(@list.find_min()).must_be_nil @@ -152,7 +152,7 @@ end end - xdescribe "delete" do + describe "delete" do it "delete from empty linked list is a no-op" do expect(@list.length).must_equal 0 @list.delete(4) From 988db904ef9267a254374c1c51beca4b52823c88 Mon Sep 17 00:00:00 2001 From: Alice Boone Date: Thu, 25 Mar 2021 16:07:57 -0700 Subject: [PATCH 3/3] add time complexity to visit 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 cf003060..33f182af 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -136,8 +136,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 return @head if @head.nil?