diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..62eb69f8 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,128 +18,299 @@ 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), will take same amount of time regardless of the length of the list + # Does not have to perform any additional operations to move the existing nodes + # Space Complexity: O(1), needs space for one new node + # Does not need any additional space for existing nodes as they are unaffected + # Would be O(n) where n is the size of the value being saved to the new node, + # but if all nodes are integers and therefore comparable sizes, n is constant def add_first(value) - raise NotImplementedError + new_node = Node.new(value) + new_node.next = @head + @head = new_node 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), in the worst case scenario needs to check every single + # node in a linked list of n length + # Space Complexity: O(n), just needs to save one node to a variable def search(value) - raise NotImplementedError + current = @head + while current + return true if current.data == value + 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: O(n). Will need to check every node in linked list of n length + # Space Complexity: O(1). Will need two running variables regardless of length of list def find_max - raise NotImplementedError + return nil if @head.nil? + current = @head + max = @head.data + + while current + max = current.data > max ? current.data : max + current = current.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). Will need to check every node in linked list of n length + # Space Complexity: O(1). Will need two running variables regardless of length of list def find_min - raise NotImplementedError + return nil if @head.nil? + current = @head + max = @head.data + + while current + max = current.data < max ? current.data : max + current = current.next + end + + return max 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), checking the head node takes one operation + # Space Complexity: O(1) assuming all nodes have comparable data sizes, same size return value regardless def get_first - raise NotImplementedError + @head.nil? ? nil : @head.data end # method that inserts a given value as a new last node in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n), will need to iterate over entire linked list of length n + # Space Complexity: O(1) def add_last(value) - raise NotImplementedError + if @head.nil? + @head = Node.new(value) + else + current = @head + while current.next + current = current.next + end + new_node = Node.new(value) + current.next = new_node + end end # method that returns the length of the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n), will need to iterate over entire linked list of length n + # Space Complexity: O(1) def length - raise NotImplementedError + return 0 if @head == nil + + count = 1 + current = @head + + while current.next + count += 1 + current = current.next + end + + return count 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), will increase linearly with the size of the index being sought, + # will potentially have to iterate through entire linked list if item sought is last or does not exist + # Space Complexity: O(1) def get_at_index(index) - raise NotImplementedError + return nil if @head.nil? + + counter = 0 + current = @head + + until counter == index + return nil if current.nil? + counter += 1 + current = current.next + end + + return current.data end # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n), will have to check each node in linked list of length n + # Space Complexity: O(n), will have to store values of n number of nodes in linked list of length n def visit - raise NotImplementedError + values = [] + current = @head + + while current + values.push(current.data) + current = current.next + end + + return values end # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n), has to iterate over linked list of length n to find the node to delete + # Space Complexity: O(1) def delete(value) - raise NotImplementedError + return if @head.nil? + @head = @head.next if @head.data == value + + current = @head + until current.next.nil? || current.next.data == value + current = current.next + end + + to_delete = current.next + if to_delete + current.next = to_delete.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), n = length of linked list + # Space Complexity: O(1) def reverse - raise NotImplementedError + # assign three pointers: one to head, one to head.next, one to head.next.next + p1 = @head + p2 = p1.next + p3 = p2.next + + # tell head to point to nil + p1.next = nil + + # point p2 to p1 and move all pointers forward + while p3 + p2.next = p1 + p1 = p2 + p2 = p3 + p3 = p3.next + end + + # last link + p2.next = p1 + + # once p3 is nil, p2 is pointing at the (former) tail, now head + @head = p2 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), n = length of linked list + # Space Complexity: O(1) def get_last - raise NotImplementedError + return nil if @head.nil? + + current = @head + while current.next + 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), n = length of linked list + # Space Complexity: O(1) def find_middle_value - raise NotImplementedError + # two pointers, one moving twice every iteration, the other moving once + slow = @head + fast = @head + + while fast.next + fast = fast.next + slow = slow.next + if fast.next + fast = fast.next + else + return slow.data + 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), n = length of linked list + # Space Complexity: O(1) def find_nth_from_end(n) - raise NotImplementedError + count = 0 + lead = @head + tail = @head + + until count == n + return nil if lead.nil? + lead = lead.next + count += 1 + end + + return nil if lead.nil? + + until lead.next.nil? + tail = tail.next + lead = lead.next + end + + return tail.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), n = length of linked list + # Space Complexity: O(n), n = number of nodes (length of linked list) def has_cycle - raise NotImplementedError + already_seen = [] + current = @head + + while current + return true if already_seen.include? current + + already_seen.push(current) + current = current.next + 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 + if @head.nil? || value < @head.data + self.add_first(value) + return + end + + current = @head + + while current.next + if current.data <= value && value <= current.next.data + new_node = Node.new(value) + new_node.next = current.next + current.next = new_node + return + else + current = current.next + end + end + + if value >= current.data + current.next = Node.new(value) + end end # Helper method for tests diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index 77423e34..f25b3765 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -59,10 +59,10 @@ 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 + @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 @@ -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 @@ -242,4 +242,113 @@ expect(@list.get_at_index(3)).must_equal 1 end end + + describe "visit" do + it 'returns an array of all values in a list' do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + + expect(@list.visit).must_equal [4, 3, 2, 1] + end + + it 'returns an empty array for an empty list' do + expect(@list.visit).must_equal [] + end + end + + describe "find_middle_value" do + it "finds the middle value of a list with odd length" do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + @list.add_first(5) + + expect(@list.find_middle_value).must_equal 3 + end + + it "finds the middle value of a list with even length" do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.add_first(4) + @list.add_first(5) + @list.add_first(6) + + expect(@list.find_middle_value).must_equal 3 + end + end + + describe "has_cycle" do + it "will return true when a cycle is present" do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + @list.create_cycle + + expect(@list.has_cycle).must_equal true + end + + it "will return false when no cycle is present" do + @list.add_first(1) + @list.add_first(2) + @list.add_first(3) + + expect(@list.has_cycle).must_equal false + end + + it "returns true when the cycle contains only one node" do + @list.add_first(1) + @list.create_cycle + + expect(@list.has_cycle).must_equal true + end + + it "returns false if the list is empty" do + expect(@list.has_cycle).must_equal false + end + end + + describe "insert_ascending" do + it "inserts and element in the middle of the list" do + @list.add_first(5) + @list.add_first(4) + @list.add_first(2) + @list.add_first(1) + + @list.insert_ascending(3) + + expect(@list.get_at_index(2)).must_equal 3 + end + + it "inserts an element at the beginning of the list" do + @list.add_first(5) + @list.add_first(4) + @list.add_first(3) + @list.add_first(2) + + @list.insert_ascending(1) + + expect(@list.get_at_index(0)).must_equal 1 + end + + it "inserts an element at the end of the list" do + @list.add_first(5) + @list.add_first(4) + @list.add_first(2) + @list.add_first(1) + + @list.insert_ascending(6) + + expect(@list.get_at_index(4)).must_equal 6 + end + + it "inserts an element in an empty list" do + @list.insert_ascending(7) + + expect(@list.get_at_index(0)).must_equal 7 + end + end end