diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..54f27282 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -14,136 +14,284 @@ def initialize(value, next_node = nil) class LinkedList def initialize @head = nil # keep the head private. Not accessible outside this class + @tail = nil + @length = 0 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 + # Time Complexity: O(1) + # Space Complexity: O(1) def add_first(value) - raise NotImplementedError + new_node = Node.new(value) + if !@head + @head = new_node + @tail = new_node + else + new_node = Node.new(value) + new_node.next = @head + @head = new_node + end + @length += 1 + return 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) where n is the number of nodes + # Space Complexity: O(1) def search(value) - raise NotImplementedError + curr = @head + while curr + return true if curr.data == value + curr = curr.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 number of nodes + # Space Complexity: O(1) def find_max - raise NotImplementedError + return if !@head + curr = @head + max = curr.data + while curr + if max < curr.data + max = curr.data + end + curr = curr.next + 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 number of nodes + # Space Complexity: O(1) def find_min - raise NotImplementedError + return if !@head + curr = @head + min = curr.data + while curr + if min > curr.data + min = curr.data + end + curr = curr.next + end + return min end # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(1) (was O(n) updated class to keep track on length) + # Space Complexity: O(1) def length - raise NotImplementedError + # count = 0 + # curr = @head + # while curr && @head + # curr = curr.next + # count += 1 + # end + # return count + return @length 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 number of nodes + # Space Complexity: O(1) def get_at_index(index) - raise NotImplementedError + target_node = node_at_index(index) + return if !target_node + return target_node.data end - # method to print all the values in the linked list - # Time Complexity: - # Space Complexity + def node_at_index(index) + length = length() + return if length - 1 < index || index < 0 + curr = @head + count = 0 + while index != count + curr = curr.next + count += 1 + end + return curr + end + + # method to return a string of all the values in the linked list + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def visit - raise NotImplementedError + node_values = "" + curr = @head + while curr + node_values << curr.data.to_s + curr = curr.next + end + return node_values end # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) where n is the number of nodes + # Space Complexity: O(1) def delete(value) - raise NotImplementedError + return if !@head + curr = @head + if curr.data == value + delete_first_node() + else + while curr.next + if curr.next.data == value + remove_next_node(curr) + else + curr = curr.next + end + end + end + @length -= 1 + return + end + + def delete_first_node + temp = @head + @head = @head.next + temp.next = nil + end + + def remove_next_node(node) + temp = node.next + node.next = node.next.next + temp.next = nil + @tail = node if !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 + # Time Complexity: O(n) + # Space Complexity: passing pointer not list to rec statck so O(n) def reverse - raise NotImplementedError + return if !@head + curr = @head + @tail = curr + reverse_links(curr) end + def reverse_links(curr) + if !curr.next + @head = curr + return + end + reverse_links(curr.next) + curr.next.next = curr + curr.next = nil + return + 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 + fast,slow = @head, @head + while fast.next && fast.next.next + fast = fast.next.next + slow = slow.next + 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 + target_node = node_at_index(length() - n - 1) + return if !target_node + return target_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: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O(1) def has_cycle - raise NotImplementedError + return false if !@head + fast, slow = @head, @head + while fast.next.next + fast = fast.next.next + slow = slow.next + if fast == slow + 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: - # Space Complexity + # Time Complexity: O(1) + # Space Complexity: O(1) def get_first - raise NotImplementedError + return @head.data if @head + return end # method that inserts a given value as a new last node in the linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(1) + # Space Complexity: O(1) def add_last(value) - raise NotImplementedError + new_node = Node.new(value) + if @tail + @tail.next = new_node + @tail = new_node + else + @head = new_node + @tail = new_node + end + @length += 1 + return 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(1) + # Space Complexity: O(1) def get_last - raise NotImplementedError + return if !@tail + return @tail.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) + # Space Complexity: O(1) def insert_ascending(value) - raise NotImplementedError + new_node = Node.new(value) + if !@head || value <= @head.data + new_node.next = @head + @head = new_node + @tail = new_node + @length += 1 + return + end + curr = @head + while curr.next && curr.next.data <= value + curr = curr.next + end + temp = curr.next + @tail = new_node if !curr.next + curr.next = new_node + new_node.next = temp + @length += 1 + return end # Helper method for tests diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index a7ee3769..5bb81d53 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -238,4 +238,133 @@ expect(@list.find_nth_from_end(3)).must_equal 4 end end + + describe "search" do + it "will return false if list is empty" do + expect(@list.search(20)).must_equal false + end + + it "will return false if value is not in list" do + + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + + expect(@list.search(20)).must_equal false + end + + it "will return true if value is found" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + + expect(@list.search(2)).must_equal true + expect(@list.search(3)).must_equal true + expect(@list.search(1)).must_equal true + expect(@list.search(4)).must_equal true + end + end + + describe "visit" do + it "will return empty string if list is empty" do + expect(@list.visit).must_equal "" + end + + it "will return all the values in order as a string" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + expect(@list.visit).must_equal "1234" + + end + end + + describe "find_middle_value" do + it "will return nil if list is empty" do + expect(@list.find_middle_value).must_be_nil + end + + it "will return the middle value if list length is odd" do + @list.add_first(5) + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + expect(@list.find_middle_value).must_equal 3 + + end + + it "will return the first value left of center if no middle value (ie list length is even)" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.add_first(1) + expect(@list.find_middle_value).must_equal 2 + end + end + + describe "has_cycle" do + it "will return false if list is empty" do + expect(@list.has_cycle).must_equal false + end + + it "will return true if list has a cyle" 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 "will return true if list is length one and has a cycle" do + @list.add_first(4) + @list.create_cycle() + expect(@list.has_cycle).must_equal true + end + it "will return false if list does not have 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(value)" do + it "will add to list if list is empty" do + expect(@list.length).must_equal 0 + @list.insert_ascending(5) + expect(@list.length).must_equal 1 + expect(@list.get_first()).must_equal 5 + end + + it 'will add to front of list if less than all values' do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + expect(@list.length).must_equal 3 + @list.insert_ascending(1) + expect(@list.length).must_equal 4 + expect(@list.get_first()).must_equal 1 + + end + + it "will add to back of list if greater than all values" do + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + @list.insert_ascending(5) + expect(@list.length).must_equal 4 + expect(@list.get_last()).must_equal 5 + end + + it "will add in middle in correct order" do + end + end + end