From 6cca8651347df25d4a87555701edb90aabbbfcd0 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Sun, 25 Aug 2019 22:46:13 -0700 Subject: [PATCH 1/5] can add new first node --- lib/linked_list.rb | 258 +++++++++++++++++++++++---------------------- 1 file changed, 131 insertions(+), 127 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..141137c6 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -12,152 +12,156 @@ 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 + def initialize + @head = nil # keep the head private. Not accessible outside this class + 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 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) + current = @head + new_node = Node.new(value, current) + @head = new_node + return @head.data + end - # method to return the max value in the linked list - # returns the data value and not the node - # Time Complexity: - # Space Complexity - def find_max - 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 min value in the linked list - # returns the data value and not the node - # Time Complexity: - # Space Complexity - def find_min - raise NotImplementedError - end + # method to return the max value in the linked list + # returns the data value and not the node + # Time Complexity: + # Space Complexity + 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 - # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity - def length - 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 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 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 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 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 + ## 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 - ## 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 - # 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 - # 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 + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + # Time Complexity: + # Space Complexity + def get_first + if @head == nil + return nil + else + return @head.data end + 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 - # 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 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 - # 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 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 - # 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 + # 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 - # 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 + # navigate to last node + current = @head + while current.next != nil + current = current.next 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 + current.next = @head # make the last node link to first node + end end From f707ca0cbab6e4341de1124801b5c7f15fb4946d Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Mon, 26 Aug 2019 08:13:12 -0700 Subject: [PATCH 2/5] more methods --- lib/linked_list.rb | 148 +++++++++++--- test/linked_list_test.rb | 409 +++++++++++++++++++-------------------- 2 files changed, 325 insertions(+), 232 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 141137c6..16c53329 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -22,6 +22,7 @@ def initialize # Space Complexity: O(1) def add_first(value) current = @head + new_node = Node.new(value, current) @head = new_node return @head.data @@ -29,42 +30,98 @@ 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) where n is the length of the linked list + # Space Complexity: O(1) def search(value) - raise NotImplementedError + current = @head + + while current != nil + if current.data == value + return true + end + 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 - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the length of the linked lisst + # Space Complexity: O(1) def find_max - raise NotImplementedError + current = @head + + if current == nil + return nil + end + + max = current.data + + while current != nil + if current.data > max + max = current.data + end + end + + return max 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) where n is the length of the linked list + # Space Complexity: O(1) def find_min - raise NotImplementedError + current = @head + + if current == nil + return nil + end + + min = current.data + + while current != nil + if current.data < min + min = current.data + end + end + + return min end # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the length of the linked list + # Space Complexity: O(1) def length - raise NotImplementedError + current = @head + i = 0 + + while current != nil + i += 1 + current = current.next + end + + return i 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) where n is the length of the linked list + # Space Complexity: O(1) def get_at_index(index) - raise NotImplementedError + i = index + current = @head + + while i > 0 && current != nil + i -= 1 + current = current.next + end + + if i > 0 + return nil + else + return current.data + end end # method to print all the values in the linked list @@ -78,7 +135,20 @@ def visit # Time Complexity: # Space Complexity def delete(value) - raise NotImplementedError + current = @head + last = current + + if current == nil + return nil + end + + while current.next != nil + if value == current.data + last.next = current.next + end + last = current + current = current.next + end end # method to reverse the singly linked list @@ -117,8 +187,8 @@ def has_cycle # 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 if @head == nil return nil @@ -128,26 +198,50 @@ def get_first end # method that inserts a given value as a new last node in the linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the length of the linked list + # Space Complexity: O(1) def add_last(value) - raise NotImplementedError + current = @head + new_node = Node.new(value, nil) + + while current.next != nil + current = current.next + end + + current.next = new_node 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) where n is the length of the linked list + # Space Complexity: O(1) def get_last - raise NotImplementedError + current = @head + + if current == nil + return nil + end + + while current.next != nil + current = current.next + end + + return current.data 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) where n is the length of the linked list + # Space Complexity: O(1) def insert_ascending(value) - raise NotImplementedError + current = @head + + while current != nil + if value > current.data + new_node = Node.new(value, current.next.next) + current.next = new_node + end + end end # Helper method for tests diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 8311c439..b8c77ba8 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -1,236 +1,235 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' - -require_relative 'test_helper' +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" +require_relative "test_helper" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe LinkedList do - # Arrange - before do - @list = LinkedList.new - end + # Arrange + before do + @list = LinkedList.new + end - describe 'initialize' do - it 'can be created' do + describe "initialize" do + it "can be created" do - # Assert - expect(@list).must_be_kind_of LinkedList - end + # Assert + expect(@list).must_be_kind_of LinkedList end + end - describe 'add_first & get_first' do - it 'can add values to an empty list' do - # Act - @list.add_first(3) + describe "add_first & get_first" do + it "can add values to an empty list" do + # Act + @list.add_first(3) - # Assert - expect(@list.get_first).must_equal 3 - end + # Assert + expect(@list.get_first).must_equal 3 + end - it 'will put the last added item to the front of the list' do - # Act - @list.add_first(1) - @list.add_first(2) + it "will put the last added item to the front of the list" do + # Act + @list.add_first(1) + @list.add_first(2) - # Assert - expect(@list.get_first).must_equal 2 + # Assert + expect(@list.get_first).must_equal 2 - # Act again - @list.add_first(3) + # Act again + @list.add_first(3) - # Assert - expect(@list.get_first).must_equal 3 - end + # Assert + expect(@list.get_first).must_equal 3 + end - it 'will return `nil` for `getFirst` if the list is empty' do - # Act-Assert - expect(@list.get_first).must_be_nil - end + it "will return `nil` for `getFirst` if the list is empty" do + # Act-Assert + expect(@list.get_first).must_be_nil end + end - describe "length" do - it "will return 0 for an empty list" do - # Act-Assert - expect(@list.length).must_equal 0 - end - - it "will return the length for nonempty lists" do - # Arrange - count = 0 - while count < 5 - @list.add_first(count) - count += 1 - # Act-Assert - expect(@list.length).must_equal count - end - end + describe "length" do + it "will return 0 for an empty list" do + # Act-Assert + expect(@list.length).must_equal 0 end - describe "addLast & getLast" do - it "will add to the front if the list is empty" do - # Arrange - @list.add_last(1) - # Act-Assert - expect(@list.get_first).must_equal 1 - end - - it "will put new items to the rear of the list" do - # Arrange - @list.add_last(2) - # Act-Assert - expect(@list.length).must_equal 1 - expect(@list.get_last).must_equal 2 - - # Arrange - @list.add_last(3) - # Act-Assert - expect(@list.get_first).must_equal 2 - expect(@list.get_last).must_equal 3 - expect(@list.length).must_equal 2 - - @list.add_last(4) - expect(@list.get_first).must_equal 2 - expect(@list.get_last).must_equal 4 - expect(@list.length).must_equal 3 - end + it "will return the length for nonempty lists" do + # Arrange + count = 0 + while count < 5 + @list.add_first(count) + count += 1 + # Act-Assert + expect(@list.length).must_equal count + end + end + end + + describe "addLast & getLast" do + it "will add to the front if the list is empty" do + # Arrange + @list.add_last(1) + # Act-Assert + expect(@list.get_first).must_equal 1 end - describe 'get_at_index' do - it 'returns nil if the index is outside the bounds of the list' do - # Act-Assert - expect(@list.get_at_index(3)).must_be_nil - end - - it 'can retrieve an item at an index in the list' do - # Arrange - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - # Act-Assert - expect(@list.get_at_index(0)).must_equal 4 - expect(@list.get_at_index(1)).must_equal 3 - expect(@list.get_at_index(2)).must_equal 2 - expect(@list.get_at_index(3)).must_equal 1 - end + it "will put new items to the rear of the list" do + # Arrange + @list.add_last(2) + # Act-Assert + expect(@list.length).must_equal 1 + expect(@list.get_last).must_equal 2 + + # Arrange + @list.add_last(3) + # Act-Assert + expect(@list.get_first).must_equal 2 + expect(@list.get_last).must_equal 3 + expect(@list.length).must_equal 2 + + @list.add_last(4) + expect(@list.get_first).must_equal 2 + expect(@list.get_last).must_equal 4 + expect(@list.length).must_equal 3 end + end - describe 'max and min values' do - it 'returns nil if the list is empty' do - # Act-Assert - expect(@list.find_max()).must_be_nil - expect(@list.find_min()).must_be_nil - end - - it 'can retrieve the max and min values in the list' do - # Arrange - count = 0 - while count < 5 - @list.add_first(count) - expect(@list.find_max).must_equal count - expect(@list.find_min).must_equal 0 - count += 1 - end - @list.add_last(100) - @list.add_first(-12) - - # Act-Assert - expect(@list.find_max).must_equal 100 - expect(@list.find_min).must_equal(-12) - end + describe "get_at_index" do + it "returns nil if the index is outside the bounds of the list" do + # Act-Assert + expect(@list.get_at_index(3)).must_be_nil end - describe "delete" do - it "delete from empty linked list is a no-op" do - # Assert - expect(@list.length).must_equal 0 - # Act - @list.delete(4) - # Assert - expect(@list.length).must_equal 0 - end - - it "can delete valid values from list" do - # Arrange - @list.add_last(9) - @list.add_last(10) - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - - # Act - # delete fist node (requires updating head) - @list.delete(2) - - # Assert - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 4 - expect(@list.get_last).must_equal 10 - expect(@list.find_max).must_equal 10 - expect(@list.find_min).must_equal 3 - - # Act (again) - # delete last node - @list.delete(10) - # Assert - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 3 - expect(@list.get_last).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - - # delete fist node (requires updating head) - @list.delete(4) - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 2 - expect(@list.get_last).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - end + it "can retrieve an item at an index in the list" do + # Arrange + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + + # Act-Assert + expect(@list.get_at_index(0)).must_equal 4 + expect(@list.get_at_index(1)).must_equal 3 + expect(@list.get_at_index(2)).must_equal 2 + expect(@list.get_at_index(3)).must_equal 1 end + end + + # describe 'max and min values' do + # it 'returns nil if the list is empty' do + # # Act-Assert + # expect(@list.find_max()).must_be_nil + # expect(@list.find_min()).must_be_nil + # end + + # it 'can retrieve the max and min values in the list' do + # # Arrange + # count = 0 + # while count < 5 + # @list.add_first(count) + # expect(@list.find_max).must_equal count + # expect(@list.find_min).must_equal 0 + # count += 1 + # end + # @list.add_last(100) + # @list.add_first(-12) + + # # Act-Assert + # expect(@list.find_max).must_equal 100 + # expect(@list.find_min).must_equal(-12) + # end + # end + + describe "delete" do + it "delete from empty linked list is a no-op" do + # Assert + expect(@list.length).must_equal 0 + # Act + @list.delete(4) + # Assert + expect(@list.length).must_equal 0 + end + + it "can delete valid values from list" do + # Arrange + @list.add_last(9) + @list.add_last(10) + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + + # Act + # delete fist node (requires updating head) + @list.delete(2) + + # Assert + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 4 + expect(@list.get_last).must_equal 10 + expect(@list.find_max).must_equal 10 + expect(@list.find_min).must_equal 3 + + # Act (again) + # delete last node + @list.delete(10) + # Assert + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 3 + expect(@list.get_last).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + + # delete fist node (requires updating head) + @list.delete(4) + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 2 + expect(@list.get_last).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + end + end - describe "nth_from_the_end" do - it 'returns nil if n is outside the bounds of the list' do - # Act-Assert - expect(@list.find_nth_from_end(3)).must_be_nil - end - - it 'can retrieve an item at index n from the end in the list' do - # Arrange - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - # Act-Assert - expect(@list.find_nth_from_end(0)).must_equal 1 - expect(@list.find_nth_from_end(1)).must_equal 2 - expect(@list.find_nth_from_end(2)).must_equal 3 - expect(@list.find_nth_from_end(3)).must_equal 4 - expect(@list.find_nth_from_end(4)).must_be_nil - end + describe "nth_from_the_end" do + it "returns nil if n is outside the bounds of the list" do + # Act-Assert + expect(@list.find_nth_from_end(3)).must_be_nil end - describe "reverse" do - it 'can retrieve an item at index n from the end in the list' do - # Arrange - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - @list.add_first(1) - - # Act - @list.reverse - - # Assert - expect(@list.find_nth_from_end(0)).must_equal 1 - expect(@list.find_nth_from_end(1)).must_equal 2 - expect(@list.find_nth_from_end(2)).must_equal 3 - expect(@list.find_nth_from_end(3)).must_equal 4 - end + it "can retrieve an item at index n from the end in the list" do + # Arrange + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + + # Act-Assert + expect(@list.find_nth_from_end(0)).must_equal 1 + expect(@list.find_nth_from_end(1)).must_equal 2 + expect(@list.find_nth_from_end(2)).must_equal 3 + expect(@list.find_nth_from_end(3)).must_equal 4 + expect(@list.find_nth_from_end(4)).must_be_nil + end + end + + describe "reverse" do + it "can retrieve an item at index n from the end in the list" do + # Arrange + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + + # Act + @list.reverse + + # Assert + expect(@list.find_nth_from_end(0)).must_equal 1 + expect(@list.find_nth_from_end(1)).must_equal 2 + expect(@list.find_nth_from_end(2)).must_equal 3 + expect(@list.find_nth_from_end(3)).must_equal 4 end + end end From 602f85320eaad454d7223871e54a83b90853fa2b Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Tue, 27 Aug 2019 00:22:07 -0700 Subject: [PATCH 3/5] got some more working --- lib/linked_list.rb | 67 +++++++++++++++++++++++++++++++++------- test/linked_list_test.rb | 48 ++++++++++++++-------------- 2 files changed, 79 insertions(+), 36 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 16c53329..8121aafb 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -61,6 +61,7 @@ def find_max if current.data > max max = current.data end + current = current.next end return max @@ -83,6 +84,7 @@ def find_min if current.data < min min = current.data end + current = current.next end return min @@ -132,14 +134,16 @@ def visit end # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the length of the linke list + # Space Complexity: O(1) def delete(value) current = @head - last = current + last = nil if current == nil return nil + elsif value == current.data + @head = current.next end while current.next != nil @@ -153,26 +157,60 @@ 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) where n is the length of the linked list + # Space Complexity: O(1) def reverse - raise NotImplementedError + current = @head + last_node = nil + next_node = current.next + + if current == nil + return nil + end + + while current.next != nil + current.next = last_node + last_node = current + current = next_node + next_node = current.next + end + + current.next = last_node + @head = current + return @head.data end ## Advanced Exercises # returns the value at the middle element in the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the length of the linked list + # Space Complexity: O(1) def find_middle_value - raise NotImplementedError + slow = @head + fast = @head + + if @head == nil + return nil + end + + while fast != nil && fast.next != nil + fast = fast.next.next + slow = slow.next + end + return slow 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) where n is the length of the linked list + # Space Complexity: O(1) def find_nth_from_end(n) - raise NotImplementedError + i = self.length - n - 1 + + if i > self.length - 1 + return nil + else + return self.get_at_index(i) + end end # checks if the linked list has a cycle. A cycle exists if any node in the @@ -204,6 +242,11 @@ def add_last(value) current = @head new_node = Node.new(value, nil) + if current == nil + @head = new_node + return @head + end + while current.next != nil current = current.next end diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index b8c77ba8..08254271 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -118,30 +118,30 @@ end end - # describe 'max and min values' do - # it 'returns nil if the list is empty' do - # # Act-Assert - # expect(@list.find_max()).must_be_nil - # expect(@list.find_min()).must_be_nil - # end - - # it 'can retrieve the max and min values in the list' do - # # Arrange - # count = 0 - # while count < 5 - # @list.add_first(count) - # expect(@list.find_max).must_equal count - # expect(@list.find_min).must_equal 0 - # count += 1 - # end - # @list.add_last(100) - # @list.add_first(-12) - - # # Act-Assert - # expect(@list.find_max).must_equal 100 - # expect(@list.find_min).must_equal(-12) - # end - # end + describe "max and min values" do + it "returns nil if the list is empty" do + # Act-Assert + expect(@list.find_max()).must_be_nil + expect(@list.find_min()).must_be_nil + end + + it "can retrieve the max and min values in the list" do + # Arrange + count = 0 + while count < 5 + @list.add_first(count) + expect(@list.find_max).must_equal count + expect(@list.find_min).must_equal 0 + count += 1 + end + @list.add_last(100) + @list.add_first(-12) + + # Act-Assert + expect(@list.find_max).must_equal 100 + expect(@list.find_min).must_equal(-12) + end + end describe "delete" do it "delete from empty linked list is a no-op" do From 88d21cb2cec55a144011455d54da4e244dcfb685 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Wed, 28 Aug 2019 01:17:10 -0700 Subject: [PATCH 4/5] all but delete --- lib/linked_list.rb | 77 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 8121aafb..7c602703 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -127,34 +127,58 @@ def get_at_index(index) end # method to print all the values in the linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the length of the linked list + # Space Complexity: O(1) def visit - raise NotImplementedError + current = @head + if !current + puts "no values" + else + while current != nil + puts current.value + current = current.next + end + end end # method to delete the first node found with specified value # Time Complexity: O(n) where n is the length of the linke list # Space Complexity: O(1) def delete(value) + return if @head == nil + current = @head last = nil - if current == nil - return nil - elsif value == current.data - @head = current.next - end - - while current.next != nil + until current == nil if value == current.data - last.next = current.next + if @head.data == value + @head = current.next + else + last.next = last.next.next + end end - last = current - current = current.next end + + last = current + current = current.next end + # return if !@head + + # previous = nil + # current = @head + + # until !current + # if current.data == value + # @head.data == value ? @head = current.next : previous.next = current.next + # end + + # previous = current + # 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) where n is the length of the linked list @@ -204,22 +228,35 @@ def find_middle_value # Time Complexity: O(n) where n is the length of the linked list # Space Complexity: O(1) def find_nth_from_end(n) + return if self.length <= n + i = self.length - n - 1 + current = @head - if i > self.length - 1 - return nil - else - return self.get_at_index(i) + i.times do + current = current.next end + + return current.data 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) where n is the length of the linked list + # Space Complexity: O(1) def has_cycle - raise NotImplementedError + current = @head + visited = [] + + while current + if visited.include?(current) + return true + end + visited << current + current = current.next + end + return false end # Additional Exercises From d748d8082953221789040cb2bd3dc2be2b03c319 Mon Sep 17 00:00:00 2001 From: Amy Wyatt Date: Thu, 5 Sep 2019 15:41:04 -0700 Subject: [PATCH 5/5] delete --- lib/linked_list.rb | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 7c602703..f8b96c25 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -128,7 +128,7 @@ def get_at_index(index) # method to print all the values in the linked list # Time Complexity: O(n) where n is the length of the linked list - # Space Complexity: O(1) + # Space Complexity: O(n) where n is the length of the linked list def visit current = @head if !current @@ -145,40 +145,21 @@ def visit # Time Complexity: O(n) where n is the length of the linke list # Space Complexity: O(1) def delete(value) - return if @head == nil + return if !@head + previous = nil current = @head - last = nil - - until current == nil - if value == current.data - if @head.data == value - @head = current.next - else - last.next = last.next.next - end + + until !current + if current.data == value + @head.data == value ? @head = current.next : previous.next = current.next end - end - last = current - current = current.next + previous = current + current = current.next + end end - # return if !@head - - # previous = nil - # current = @head - - # until !current - # if current.data == value - # @head.data == value ? @head = current.next : previous.next = current.next - # end - - # previous = current - # 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) where n is the length of the linked list @@ -243,7 +224,7 @@ def find_nth_from_end(n) # 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) where n is the length of the linked list + # Time Complexity: O(n^2) where n is the length of the linked list # Space Complexity: O(1) def has_cycle current = @head