diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..d44ae309 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -12,152 +12,309 @@ 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: O(1) + # Space Complexity: O(1) + def add_first(value) + new_node = Node.new(value, @head) + @head = new_node + 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 + # 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 + + until current.next == nil + return true if value == current.data + + current = current.next 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 + 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 nil if !@head + + current = @head.next + max_value = @head.data + + until !current + max_value = current.data if current.data > max_value + current = current.next 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 + return max_value + 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(n) + def find_min + return nil if !@head + + current = @head.next + min_value = @head.data + + until !current + min_value = current.data if current.data < min_value + current = current.next 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 + return min_value + 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 + + count = 1 + current = @head.next + + until !current + count += 1 + current = current.next end + return count + end - # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity - def length - 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) + count = 0 + current = @head + while current != nil + if count == index + return current.data + end + current = current.next + count += 1 end + return nil + end + + # method to print all the values in the linked list + # Time Complexity: O(n) + # Space Complexity: O(n) + def visit + return if @head == nil - # 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 + current = @head + + until current.next == nil + current = current.next end + end - # method to print all the values in the linked list - # Time Complexity: - # Space Complexity - def visit - raise NotImplementedError + # method to delete the first node found with specified value + # Time Complexity: O(n) + # Space Complexity: O(1) + def delete(value) + return if !@head + + if @head.data == value + @head = @head.next + return end - # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity - def delete(value) - raise NotImplementedError + previous = @head + current = @head.next + + until value == current.data || !current + previous = current + current = current.next 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 + previous.next = current.next + 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 nil if @head == nil + + current = @head + next_node = nil + previous_node = nil + + until current == nil + next_node = current.next + current.next = previous_node + previous_node = current + current = next_node end + @head = previous_node + 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 + fast = @head + slow = @head - ## Advanced Exercises - # returns the value at the middle element in the singly linked list - # Time Complexity: - # Space Complexity - def find_middle_value - raise NotImplementedError + while fast != nil && fast.next != nil + slow = slow.next + fast = fast.next.next + if fast.next == nil + return slow.data + 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: O(n) + # Space Complexity: O(1) + def find_nth_from_end(n) + return nil if @head == nil + + fast = @head + slow = @head + count = 0 - # 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 + until count == n + fast = fast.next + return nil if fast == nil + count += 1 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 + until fast.next == nil + fast = fast.next + slow = slow.next end + return slow.data + 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 + # 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 + slow = @head + fast = @head + while fast != nil && fast.next != nil + slow = slow.next + fast = fast.next.next + if slow == fast + return true + end end + return false + end + + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + # Time Complexity: O(n) + # Space Complexity: O(1) + def get_first + return nil if !@head + + @head.data + 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) + return self.add_first(value) if !@head - # 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 + new_node = Node.new(value) + + current = @head + + until !current.next + current = current.next 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 + 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: O(n) + # Space Complexity: O(1) + def get_last + return nil if !@head + return @head.data if !@head.next + + current = @head.next + + until !current.next + current = current.next 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 + 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: O(n) + # Space Complexity: O(1) + def insert_ascending(value) + new_node = Node.new(value) + if @head == nil + @head = new_node + return @head.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 + if @head.data > new_node.data + temp_node = @head + @head = new_node + new_node.next = temp_node + return @head.data + end - # navigate to last node - current = @head - while current.next != nil - current = current.next + curr_node = @head + while curr_node != nil + if curr_node.data <= new_node.data && curr_node.next.data > new_node.data + temp_node = curr_node.next + curr_node.next = new_node + new_node.next = temp_node + return new_node.data end + curr_node = curr_node.next + end + end - current.next = @head # make the last node link to first node + # 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 a7ee3769..d97e3306 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -1,241 +1,240 @@ -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 - - describe 'initialize' do - it 'can be created' do - - # 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) - - # 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) - - # Assert - expect(@list.get_first).must_equal 2 - - # Act again - @list.add_first(3) - - # 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 - 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 - 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 `nil` for `get_last` if the list is empty" do - # Act-Assert - expect(@list.get_last).must_be_nil - end - 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 - 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 - 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 + # Arrange + before do + @list = LinkedList.new + end + + describe "initialize" do + it "can be created" do + + # 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) + + # 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) + + # Assert + expect(@list.get_first).must_equal 2 + + # Act again + @list.add_first(3) + + # 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 + 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 + 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 `nil` for `get_last` if the list is empty" do + # Act-Assert + expect(@list.get_last).must_be_nil + end + 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 + 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 + 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