diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..6206500f 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -1,4 +1,3 @@ - # Defines a node in the singly linked list class Node attr_reader :data # allow external entities to read value but not write @@ -12,148 +11,352 @@ 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) + # >> Constant num of actions - create new node, + # set next to old head, set head pointer to new node + # Space Complexity: O(1) + # >> Always adding 1 new node + def add_first(value) + @head = Node.new(value, @head) + 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) + # >> Must check every single node in linked list + # Space Complexity: O(1) + # >> Just checking for membership; only ever store current_node + def search(value) + current_node = @head + + until current_node.nil? + return true if current_node.data == value + + current_node = current_node.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 + def find_max + return nil if @head.nil? + + current_node = @head + max = current_node.data + + until current_node.nil? + max = current_node.data if current_node.data > max + current_node = current_node.next end - # method to return the max value in the linked list - # returns the data value and not the node - def find_max - raise NotImplementedError + return max + end + + # method to return the min value in the linked list + # returns the data value and not the node + # Time Complexity: O(n) + # >> have to traverse entire linked list (unless we know it's sorted...) + # Space Complexity: O(1) + # >> store current_node and min regardless of how long LL is + def find_min + return nil if @head.nil? + + current_node = @head + min = current_node.data + + until current_node.nil? + min = current_node.data if current_node.data < min + current_node = current_node.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 + end + + + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + # Time Complexity: O(1) + # >> pointer to head + # Space Complexity: O(1) + # >> don't need to allocate space for vars - just returning value of first node if exists + def get_first + return nil if @head.nil? + + return @head.data + end + + # method that inserts a given value as a new last node in the linked list + # Time Complexity: O(n) + # >> Have to continue traversing linked list until reach last node (next is nil) + # >> Conversely, if we had a tail pointer, then this could be O(1) in a doubly-linked list + # Space Complexity: O(1) + # >> adding one new node each time (reference in next for an existing node, add new node) + def add_last(value) + if @head.nil? + @head = Node.new(value) + return end + current_node = @head - # 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 + until current_node.next.nil? + current_node = current_node.next 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 + current_node.next = Node.new(value) + end + + # method that returns the length of the singly linked list + # Time Complexity: O(n) + # >> traverse length of ll to get num nodes in list + # Space Complexity: O(1) + # >> store len integer + def length + return 0 if @head.nil? + + len = 1 + current_node = @head.next + + until current_node.nil? + len += 1 + current_node = current_node.next end - # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? - def length - raise NotImplementedError + return len + 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) + # >> Traverse up to n nodes to get value at index position + # Space Complexity: O(1) + # >> store node_num and current_node + def get_at_index(index) + return nil if @head.nil? + return @head.data if index.zero? + + node_num = 0 + current_node = @head + + until node_num == index || current_node.nil? + current_node = current_node.next + node_num += 1 unless current_node.nil? 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 + return node_num == index ? current_node.data : nil + end + + # method to print all the values in the linked list + # Time Complexity: O(n) + # >> Traverse entire LL to print all values + # Space Complexity: O(1) + # >> store current node + def visit + return nil if @head.nil? + + current_node = @head + + until current_node.nil? + p current_node.data + current_node = current_node.next end + end + + # method to delete the first node found with specified value + # Time Complexity: O(n) + # >> search up to entire list to find node with specified value + # Space Complexity: O(1) + # >> keep track of current node + def delete(value) + return nil if @head.nil? - # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? - def visit - raise NotImplementedError + if @head.data == value + new_head = @head.next + @head.next = nil + @head = new_head end - # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? - def delete(value) - raise NotImplementedError + current_node = @head + until current_node.next.nil? + if current_node.next.data == value + current_node.next = current_node.next.next + return + end + current_node = current_node.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 + return nil + 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) + # >> traverse length of LL to reverse all nodes + # Space Complexity: O(1) + # >> store previous, current, and next nodes + def reverse + return nil if @head.nil? + + current_node = @head + previous_node = nil + next_node = nil + + while current_node + next_node = current_node.next + current_node.next = previous_node + previous_node = current_node + current_node = next_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: ? - def get_last - raise NotImplementedError + @head = previous_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) + # >> traverse length of LL to get to last node + # Space Complexity: O(1) + # >> store current node + def get_last + return nil if @head.nil? + + current_node = @head + + until current_node.next.nil? + current_node = current_node.next end + + return current_node.data + 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 + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + # Time Complexity: O(n) + # >> get length of LL (pass through all n nodes) + # >> go through half of those nodes + # Space Complexity: O(1) + # >> store mid_index, current_node, current_node_num + def find_middle_value + return nil if @head.nil? + + mid_index = length / 2 + current_node = @head + current_node_num = 0 + + until current_node_num == mid_index + current_node = current_node.next + current_node_num += 1 + end + + return current_node.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) + # >> pass through all nodes to get length + # >> at worst pass through all nodes again to get 0th node from end + # >> 2n --> n + # Space Complexity: O(1) + # >> store len, index, current_node, curr_node_num + def find_nth_from_end(n) + return nil if @head.nil? + + len = length + return nil if n >= len # maybe better to throw ArgumentError here + + index = len - n - 1 + current_node = @head + curr_node_num = 0 + until curr_node_num == index + current_node = current_node.next + curr_node_num += 1 + end + + return current_node.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: O(n) + # >> If no cycle, 'fast' and 'slow' pointers will go through LL once + # >> If cycle, 'slow' pointer will go through the list at most once + # >> b/c 'fast' cannot lap / overtake the 'slow' pointer if 'fast' + # >> moves two nodes at a time while 'slow' 1 node at a time + # >> if DID skip over --> slow --> fast + # >> previous step would mean both pointers were at the same node + # Space Complexity: O(1) + # >> fast and slow nodes + def has_cycle + return nil if @head.nil? + + fast = slow = @head + while fast&.next + fast = fast.next.next + slow = slow.next + + return true if slow == fast + 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) + # >> worst case node has to be placed at end + # Space Complexity: O(1) + # >> store new_node and current_node + def insert_ascending(value) + new_node = Node.new(value) + + return @head = new_node if @head.nil? + + current_node = @head + + if current_node.data > value + new_node.next = current_node + @head = new_node + return + end + + while current_node.data <= value && current_node.next + if current_node.next.data > value + new_next_node = current_node.next + current_node.next = new_node + new_node.next = new_next_node + return end - current.next = @head # make the last node link to first node + current_node = current_node.next end + + current_node.next = new_node + 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..899dc363 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -1,245 +1,304 @@ require_relative 'test_helper' - Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe LinkedList do - # Arrange - before do - @list = LinkedList.new + # 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_at_index(0)).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_at_index(0)).must_equal 2 + + # Act again + @list.add_first(3) + + # Assert + expect(@list.get_at_index(0)).must_equal 3 + end + + it 'will return `nil` for `getFirst` if the list is empty' do + expect(@list.get_at_index(0)).must_be_nil + end + end + + describe 'search' do + it 'can find an element' do + @list = LinkedList.new + @list.add_first(3) + @list.add_first(2) + + expect(@list.search(3)).must_equal true + + expect(@list.search(2)).must_equal true + end + + it 'returns false if the element is not in the list' do + @list = LinkedList.new + @list.add_first(3) + @list.add_first(2) + expect(@list.search('pasta')).must_equal false + end + + it 'returns false for an empty list' do + expect(@list.search(3)).must_equal false + end + end + + describe 'length' do + it 'will return 0 for an empty list' do + expect(@list.length).must_equal 0 + end + + it 'will return the length for nonempty lists' do + count = 0 + while count < 5 + @list.add_first(count) + count += 1 + expect(@list.length).must_equal count + end + end + end + + describe 'get_at_index' do + it 'returns nil if the index is outside the bounds of the list' do + expect(@list.get_at_index(3)).must_be_nil + end + + it 'can retrieve an item at an index in the list' do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + + 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 '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 end - describe 'initialize' do - it 'can be created' do + it 'will put new items to the rear of the list' do + @list.add_last(2) + expect(@list.length).must_equal 1 + expect(@list.get_last).must_equal 2 + + @list.add_last(3) + expect(@list.get_at_index(0)).must_equal 2 + expect(@list.get_last).must_equal 3 + expect(@list.length).must_equal 2 + + @list.add_last(4) + expect(@list.get_at_index(0)).must_equal 2 + expect(@list.get_last).must_equal 4 + expect(@list.length).must_equal 3 + end + end - # Assert - expect(@list).must_be_kind_of LinkedList - end + 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 end - describe 'add_first & get_first' do - it 'can add values to an empty list' do - # Act - @list.add_first(3) + it 'can retrieve the max and min values in the list' do + 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_first(100) + @list.add_first(-12) + expect(@list.find_max).must_equal 100 + expect(@list.find_min).must_equal(-12) + @list.add_last(99) + expect(@list.find_max).must_equal 100 + @list.add_first(50) + expect(@list.find_min).must_equal(-12) + end + end - # Assert - expect(@list.get_at_index(0)).must_equal 3 - end + describe 'delete' do + it 'delete from empty linked list is a no-op' do + expect(@list.length).must_equal 0 + @list.delete(4) + expect(@list.length).must_equal 0 + 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 'can delete valid values from list' do + @list.add_first(9) + @list.add_first(10) + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + + # delete first node (requires updating head) + @list.delete(2) + expect(@list.get_first).must_equal 3 + expect(@list.length).must_equal 4 + expect(@list.get_at_index(@list.length - 1)).must_equal 9 + expect(@list.find_max).must_equal 10 + expect(@list.find_min).must_equal 3 + + # delete last node + @list.delete(10) + expect(@list.get_at_index(0)).must_equal 3 + expect(@list.length).must_equal 3 + expect(@list.get_at_index(@list.length - 1)).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + + # delete middle node (requires updating head) + @list.delete(4) + expect(@list.get_at_index(0)).must_equal 3 + expect(@list.length).must_equal 2 + expect(@list.get_at_index(@list.length - 1)).must_equal 9 + expect(@list.find_max).must_equal 9 + expect(@list.find_min).must_equal 3 + end - # Assert - expect(@list.get_at_index(0)).must_equal 2 + it 'can delete from the middle' do + # Arrange + @list.add_last(9) + @list.add_last(10) + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + + # Act + @list.delete(9) + + # Assert + expect(@list.get_last).must_equal 10 + expect(@list.length).must_equal 4 + expect(@list.get_first).must_equal 2 + end + end - # Act again - @list.add_first(3) + 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 + + it 'can retrieve an item at index n from the end in the list' do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + + 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 + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + @list.reverse + + 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 'has_cycle' do + it 'correctly detects a cycle' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + @list.create_cycle + + expect(@list.has_cycle).must_equal true + end + + it 'correctly detects when there is not a cycle' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + + expect(@list.has_cycle).must_equal false + end + end + + describe 'insert_ascending' do + it 'can insert node at beginning of empty list' do + @list.insert_ascending(4) + + expect(@list.get_at_index(0)).must_equal 4 + end + + it 'can insert node at beginning of list where all other nodes have higher values' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + + @list.insert_ascending(0) + + expect(@list.get_first).must_equal 0 + end + + it 'can insert node at end of list' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + + @list.insert_ascending(5) + + expect(@list.get_last).must_equal 5 + end - # Assert - expect(@list.get_at_index(0)).must_equal 3 - end + it 'can insert node in middle of list' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) - it 'will return `nil` for `getFirst` if the list is empty' do + @list.insert_ascending(3) - expect(@list.get_at_index(0)).must_be_nil - end - end - - describe "search" do - it "can find an element" do - @list = LinkedList.new - @list.add_first(3) - @list.add_first(2) - - expect(@list.search(3)).must_equal true - - expect(@list.search(2)).must_equal true - end - - it "returns false if the element is not in the list" do - @list = LinkedList.new - @list.add_first(3) - @list.add_first(2) - expect(@list.search("pasta")).must_equal false - end - - it "returns false for an empty list" do - expect(@list.search(3)).must_equal false - end - end - - describe "length" do - it "will return 0 for an empty list" do - expect(@list.length).must_equal 0 - end - - it "will return the length for nonempty lists" do - count = 0 - while count < 5 - @list.add_first(count) - count += 1 - expect(@list.length).must_equal count - end - end - end - - describe 'get_at_index' do - it 'returns nil if the index is outside the bounds of the list' do - expect(@list.get_at_index(3)).must_be_nil - end - - it 'can retrieve an item at an index in the list' do - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - 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 - - xdescribe "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 - end - - it "will put new items to the rear of the list" do - @list.add_last(2) - expect(@list.length).must_equal 1 - expect(@list.get_last).must_equal 2 - - @list.add_last(3) - expect(@list.get_at_index(0)).must_equal 2 - expect(@list.get_last).must_equal 3 - expect(@list.length).must_equal 2 - - @list.add_last(4) - expect(@list.get_at_index(0)).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 - 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 - 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_first(100) - @list.add_first(-12) - expect(@list.find_max).must_equal 100 - expect(@list.find_min).must_equal(-12) - @list.add_last(99) - expect(@list.find_max).must_equal 100 - @list.add_first(50) - expect(@list.find_min).must_equal(-12) - end - end - - describe "delete" do - it "delete from empty linked list is a no-op" do - expect(@list.length).must_equal 0 - @list.delete(4) - expect(@list.length).must_equal 0 - end - - it "can delete valid values from list" do - @list.add_first(9) - @list.add_first(10) - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - - # delete first node (requires updating head) - @list.delete(2) - expect(@list.get_first).must_equal 3 - expect(@list.length).must_equal 4 - expect(@list.get_at_index(@list.length - 1)).must_equal 9 - expect(@list.find_max).must_equal 10 - expect(@list.find_min).must_equal 3 - - # delete last node - @list.delete(10) - expect(@list.get_at_index(0)).must_equal 3 - expect(@list.length).must_equal 3 - expect(@list.get_at_index(@list.length - 1)).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - - # delete middle node (requires updating head) - @list.delete(4) - expect(@list.get_at_index(0)).must_equal 3 - expect(@list.length).must_equal 2 - expect(@list.get_at_index(@list.length - 1)).must_equal 9 - expect(@list.find_max).must_equal 9 - expect(@list.find_min).must_equal 3 - end - - it "can delete from the middle" do - # Arrange - @list.add_last(9) - @list.add_last(10) - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - - # Act - @list.delete(9) - - # Assert - expect(@list.get_last).must_equal 10 - expect(@list.length).must_equal 4 - expect(@list.get_first).must_equal 2 - end - end - - xdescribe "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 - - it 'can retrieve an item at index n from the end in the list' do - @list.add_first(1) - @list.add_first(2) - @list.add_first(3) - @list.add_first(4) - - 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 - @list.add_first(4) - @list.add_first(3) - @list.add_first(2) - @list.add_first(1) - @list.reverse - - 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 + expect(@list.get_at_index(3)).must_equal 3 end + end end