From dedc35170a7ae12e077ad28253371e222efeeaef Mon Sep 17 00:00:00 2001 From: christinaminh Date: Wed, 24 Mar 2021 19:42:56 -0700 Subject: [PATCH 1/2] completed --- lib/linked_list.rb | 274 ++++++++++++++++++++++++++++++++------- test/linked_list_test.rb | 4 +- 2 files changed, 230 insertions(+), 48 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..a4194fce 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,128 +18,310 @@ 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 + if @head + new_node = Node.new(value, @head) + @head = new_node + else + @head = Node.new(value) + end 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 + return false if @head == nil + + 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 + # Time Complexity: O(n) + # Space Complexity: O(1) def find_max - raise NotImplementedError + return if @head == nil + + current = @head + max = @head + + until current.nil? + if current.data > max.data + max = current + end + + current = current.next + end + + return max.data end # 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 if @head == nil + + current = @head + min = @head + + until current.nil? + if current.data < min.data + min = current + end + + current = current.next + end + + return min.data end # 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.data if @head 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? + @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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def length - raise NotImplementedError + if @head.nil? + return 0 + else + count = 0 + + current = @head + until current.nil? + count += 1 + current = current.next + end + + return count + end 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_at_index(index) - raise NotImplementedError + return if @head == nil + + count = 0 + current = @head + + until count == index + current = current.next + count += 1 + end + + return current.data 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 if @head == nil + + current = @head + + until current.nil? + puts 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 + return + end + + current = @head + + until current.next.nil? + if current.next.data == value + current.next = current.next.next + return + end + + current = current.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 + return if @head == nil + + current = @head + previous = nil + + until current.nil? + # save state + temp = current.next + # update linked list + current.next = previous + # move to next node + previous = current + current = temp + end + + @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: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def get_last - raise NotImplementedError + return if @head == nil + + current = @head + + until current.next.nil? + current = current.next + end + + return current.data end ## 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(1) def find_middle_value - raise NotImplementedError + return if @head == nil + + slow = @head + fast = @head.next + + until fast.nil? + slow = slow.next + fast = fast.next + unless fast.nil? + fast = fast.next + end + end + + return slow.data 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def find_nth_from_end(n) - raise NotImplementedError + return if @head == nil + + count = 0 + + current = @head + trailing_current = nil + + until count == n || current.next.nil? + current = current.next + count += 1 + end + + if count == n + trailing_current = @head + end + + until current.next.nil? + current = current.next + trailing_current = trailing_current.next + end + + return trailing_current.data if trailing_current 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def has_cycle - raise NotImplementedError + return if @head == nil + + slow = @head + fast = @head.next + + until fast.nil? + slow = slow.next + fast = fast.next + unless fast.nil? + fast = fast.next + end + + if fast == slow + return true + end + end + + return false end # 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(1) def insert_ascending(value) - raise NotImplementedError + return if @head == nil + + current = @head + + until current.nil? + if current.next.data > value + current.next = Node.new(value, current.next.next) + end + end end # Helper method for tests diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 77423e34..761d3cc9 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 @@ -209,7 +209,7 @@ end end - xdescribe "Optional: nth_from_the_end" do + describe "Optional: nth_from_the_end" do it 'returns nil if n is outside the bounds of the list' do expect(@list.find_nth_from_end(3)).must_be_nil end From ed34b69b346212e4dfa84493c5465a21369f9b43 Mon Sep 17 00:00:00 2001 From: christinaminh Date: Wed, 24 Mar 2021 19:42:56 -0700 Subject: [PATCH 2/2] completed --- lib/linked_list.rb | 470 +++++++++++++++++++++++++++------------ test/linked_list_test.rb | 4 +- test/test_helper.rb | 3 +- 3 files changed, 329 insertions(+), 148 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..41fe86ee 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -12,148 +12,330 @@ 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 - - # 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 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 return the max value in the linked list - # returns the data value and not the node - def find_max - raise NotImplementedError - end - - # 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 - 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 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 - - # 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 - 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 - raise NotImplementedError - 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 - 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 - - # navigate to last node - current = @head - while current.next != nil - current = current.next - end - - current.next = @head # make the last node link to first node - 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: O(1) + # Space Complexity: O(1) + def add_first(value) + if @head + new_node = Node.new(value, @head) + @head = new_node + else + @head = Node.new(value) + end + 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) + return false if @head == nil + + 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 + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_max + return if @head == nil + + current = @head + max = @head + + until current.nil? + if current.data > max.data + max = current + end + + current = current.next + end + + return max.data + 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 + return if @head == nil + + current = @head + min = @head + + until current.nil? + if current.data < min.data + min = current + end + + current = current.next + end + + return min.data + 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.data if @head + end + + # 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 + 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: O(n) + # Space Complexity: O(1) + def length + if @head.nil? + return 0 + else + count = 0 + + current = @head + until current.nil? + count += 1 + current = current.next + end + + return count + end + 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: O(n) + # Space Complexity: O(1) + def get_at_index(index) + return if @head == nil + + count = 0 + current = @head + + until count == index + current = current.next + count += 1 + end + + return current.data + end + + # method to print all the values in the linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + def visit + return if @head == nil + + current = @head + + until current.nil? + puts current.data + current = current.next + end + end + + # method to delete the first node found with specified value + # Time Complexity: O(n) + # Space Complexity: O(1) + def delete(value) + return if @head == nil + + if @head.data == value + @head = @head.next + return + end + + current = @head + + until current.next.nil? + if current.next.data == value + current.next = current.next.next + return + end + + current = current.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: O(n) + # Space Complexity: O(1) + def reverse + return if @head == nil + + current = @head + previous = nil + + until current.nil? + # save state + temp = current.next + # update linked list + current.next = previous + # move to next node + previous = current + current = temp + end + + @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 if @head == nil + + current = @head + + until current.next.nil? + current = current.next + end + + return current.data + end + + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_middle_value + return if @head == nil + + slow = @head + fast = @head.next + + until fast.nil? + slow = slow.next + fast = fast.next + unless fast.nil? + fast = fast.next + end + end + + return slow.data + end + + # 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(1) + def find_nth_from_end(n) + return if @head == nil + + count = 0 + + current = @head + trailing_current = nil + + until count == n || current.next.nil? + current = current.next + count += 1 + end + + if count == n + trailing_current = @head + end + + until current.next.nil? + current = current.next + trailing_current = trailing_current.next + end + + return trailing_current.data if trailing_current + 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: O(n) + # Space Complexity: O(1) + def has_cycle + return if @head == nil + + slow = @head + fast = @head.next + + until fast.nil? || fast.next.nil? + slow = slow.next + fast = fast.next.next + unless fast.nil? + fast = fast.next + end + + if fast == slow + return true + end + end + + return false + end + + # 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(1) + def insert_ascending(value) + return if @head == nil + + current = @head + + until current.nil? + if current.next.data > value + current.next = Node.new(value, current.next.next) + end + end + 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 + + # navigate to last node + current = @head + while current.next != nil + current = current.next + end + + current.next = @head # make the last node link to first node + end end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 77423e34..761d3cc9 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 @@ -209,7 +209,7 @@ end end - xdescribe "Optional: nth_from_the_end" do + describe "Optional: nth_from_the_end" do it 'returns nil if n is outside the bounds of the list' do expect(@list.find_nth_from_end(3)).must_be_nil end diff --git a/test/test_helper.rb b/test/test_helper.rb index 100cecef..b1144db5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,5 +6,4 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -require_relative '../lib/linked_list' - +require_relative '../lib/linked_list' \ No newline at end of file