diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..2448b7e0 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -4,7 +4,7 @@ class Node attr_reader :data # allow external entities to read value but not write attr_accessor :next # allow external entities to read or write next node - def initialize(value, next_node = nil) + def initialize(value, next_node=nil) @data = value @next = next_node end @@ -18,73 +18,156 @@ 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 + # Space Complexity O def add_first(value) - raise NotImplementedError + new_node = Node.new(value, @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) + # Space Complexity: O def search(value) - raise NotImplementedError + current = @head + + while current + if current.data == value + return true + else + current = current.next + end + 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) + # Space Complexity: O def find_max - raise NotImplementedError + visited_node = @head + return nil if visited_node == nil + + max = visited_node.data + + while visited_node != nil + if visited_node.data > max + max = visited_node.data + end + visited_node = visited_node.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) + # Space Complexity: O def find_min - raise NotImplementedError + return nil if @head == nil + + visited_node = @head + min = visited_node.data + + while visited_node != nil + if visited_node.data < min + min = visited_node.data + end + visited_node = visited_node.next + end + return min end # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) - must count all nodes + # Space Complexity: O - constant value returned def length - raise NotImplementedError + count = 0 + current = @head + + if @head == nil + return 0 + else + until current == nil + current = current.next + count += 1 + end + 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) - worst case + # Space Complexity: O - constant value returned def get_at_index(index) - raise NotImplementedError + current = @head + + return nil if @head == nil + count = 0 + + while count < index && current.next != nil + current = current.next + count += 1 + end + + return current.data end # method to print all the values in the linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity O - just printing to console, no new memory needed def visit - raise NotImplementedError + current = @head + + while current.next != nil + puts @current.data + current = current.next + end + # for last node + puts current.data end # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity O def delete(value) - raise NotImplementedError + current = @head + + if @head == nil + return 0 + end + + # if head node has the value to delete + if current.data == value + @head = current.next + end + + while current!= nil + if current.next != nil && current.next.data == value + current.next = current.next.next + end + 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: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity O(n) def reverse - raise NotImplementedError + current = @head + reversed_list = LinkedList.new + + while current.next != nil + reversed_list.add_last(current) + current = current.next + end + return reversed_list end @@ -98,10 +181,33 @@ def find_middle_value # 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 def find_nth_from_end(n) - raise NotImplementedError + current = @head + + return nil if @head == nil + + # need to know length + length = 0 + while current != nil + current = current.next + length += 1 + end + + # return position from the end + position = length - n - 1 + return nil if position < 0 + + search_node = @head + starting_index = 0 + while search_node != nil + if starting_index == position + return search_node.data + end + starting_index += 1 + search_node = search_node.next + end end # checks if the linked list has a cycle. A cycle exists if any node in the @@ -120,14 +226,27 @@ def has_cycle # Time Complexity: # Space Complexity def get_first - raise NotImplementedError + return @head if @head.nil? + + first_node = @head + return first_node.data end # method that inserts a given value as a new last node in the linked list - # Time Complexity: + # Time Complexity:O(n) # Space Complexity def add_last(value) - raise NotImplementedError + last_node = Node.new(value) + current = @head + + if @head == nil + @head = last_node + else + while current.next != nil + current = current.next + end + current.next = last_node + end end # method that returns the value of the last node in the linked list @@ -135,7 +254,16 @@ def add_last(value) # Time Complexity: # Space Complexity def get_last - raise NotImplementedError + count = 0 + current = @head + if !current + 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 diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index a7ee3769..8741070b 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -168,7 +168,7 @@ @list.add_first(2) # Act - # delete fist node (requires updating head) + # delete first node (requires updating head) @list.delete(2) # Assert @@ -188,7 +188,7 @@ expect(@list.find_max).must_equal 9 expect(@list.find_min).must_equal 3 - # delete fist node (requires updating head) + # delete first node (requires updating head) @list.delete(4) expect(@list.get_first).must_equal 3 expect(@list.length).must_equal 2