From 04e9ca96277dc3838a7103e72e741957262a464f Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Mon, 26 Aug 2019 08:45:22 -0700 Subject: [PATCH 1/2] some of the stuff --- lib/linked_list.rb | 333 +++++++++++++++++++++++++++++---------------- 1 file changed, 216 insertions(+), 117 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..3433bb6e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -12,152 +12,251 @@ 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: - # Space Complexity - def add_first(value) - raise NotImplementedError - 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) + 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 - def search(value) - raise NotImplementedError + # 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) + node = @head + while node != nil + if node.data == value + return true + end + node = node.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 - def find_max - raise NotImplementedError + # 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 + if @head == nil + return nil 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 + max = @head.data + current = @head + while current != nil + if current.data > max + max = current.data + end + current = current.next end + return max + end - - # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity - def length - raise NotImplementedError + # 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 + if @head == nil + return 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 + node = @head + min = node.data + while node != nil + if node.data < min + min = node.data + end + node = node.next end + return min + end - # method to print all the values in the linked list - # Time Complexity: - # Space Complexity - def visit - raise NotImplementedError + # method that returns the length of the singly linked list + # Time Complexity: + # Space Complexity + def length + return 0 if !@head + length = 0 + current = @head + while current != nil + length += 1 + current = current.next end + return length + end - # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity - def delete(value) - 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: + # Space Complexity + def get_at_index(index) + length = 0 + current = @head + while current != nil + if length == index + return current.data + end + length += 1 + current = current.next end + 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: - # Space Complexity - def reverse - raise NotImplementedError + # method to print all the values in the linked list + # Time Complexity: + # Space Complexity + def visit + current = @head + while current != nil + puts current.data + current = current.next end + end + # method to delete the first node found with specified value + # Time Complexity: + # Space Complexity + def delete(value) + # if self.length != 0 + # if @head.data == value + # if @head.next != nil + # @head = @head.next + # else + # @head = nil + # end + # current = @head + # while current.next != nil + # if current.data == value + # current = current.next + # end + # length += 1 + # current = current.next + # end + # end + # return nil + 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 + # 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 + temp = @head + puts "temp #{temp} @head #{@head}" + puts "length #{self.length}" + # while temp.next != nil + # @head = temp.next + # puts "temp #{temp} @head #{@head} temp.next #{temp.next} @head.next #{@head.next}" + # @head.next = temp + # temp = temp.next + # puts "temp #{temp} @head #{@head} temp.next #{temp.next} @head.next #{@head.next}" + # end + # temp = @head + # puts "temp #{temp} @head #{@head}" + 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 + ## 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 - # 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 + # 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 - # 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 + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + # Time Complexity: + # Space Complexity + def get_first + if @head == nil + return nil 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 + if @head.data != nil + return @head.data + else + return nil end + 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 + # method that inserts a given value as a new last node in the linked list + # Time Complexity: + # Space Complexity + def add_last(value) + puts value + last = Node.new(value) + if @head == nil + @head = last + else + current = @head + while current.next != nil + current = current.next + end + current.next = last end + 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 + # 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 + return nil if @head == nil + val = @head.data + current = @head + while current != nil + val = current.data + current = current.next end + return val + 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 + # 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 - # navigate to last node - current = @head - while current.next != nil - current = current.next - 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 - current.next = @head # make the last node link to first node + # 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 From f90b4b8bc132e45b2f597903807a8ce809490d7d Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Tue, 3 Sep 2019 07:03:19 -0700 Subject: [PATCH 2/2] all passing --- lib/linked_list.rb | 117 +++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 57 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 3433bb6e..e7ae20d1 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,8 +18,8 @@ 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) + # Space Complexity n? def add_first(value) new_node = Node.new(value) new_node.next = @head @@ -28,8 +28,8 @@ def add_first(value) # 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(1) def search(value) node = @head while node != nil @@ -43,8 +43,8 @@ def search(value) # 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(1) def find_max if @head == nil return nil @@ -62,8 +62,8 @@ def find_max # 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(1) def find_min if @head == nil return nil @@ -80,8 +80,8 @@ def find_min end # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity O(1) def length return 0 if !@head length = 0 @@ -96,8 +96,8 @@ def length # 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) + # Space Complexity O(1) def get_at_index(index) length = 0 current = @head @@ -112,8 +112,8 @@ def get_at_index(index) end # method to print all the values in the linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity O(1) def visit current = @head while current != nil @@ -123,45 +123,41 @@ def visit end # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity constant def delete(value) - # if self.length != 0 - # if @head.data == value - # if @head.next != nil - # @head = @head.next - # else - # @head = nil - # end - # current = @head - # while current.next != nil - # if current.data == value - # current = current.next - # end - # length += 1 - # current = current.next - # end - # end - # return nil + if @head == nil + return nil + end + if @head.data == value + @head = @head.next + else + current = @head + while current.next != nil + if current.next.data == value + current.next = current.next.next + break + end + current = current.next + end + 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 almost constant, needs three pointers allocated def reverse - temp = @head - puts "temp #{temp} @head #{@head}" - puts "length #{self.length}" - # while temp.next != nil - # @head = temp.next - # puts "temp #{temp} @head #{@head} temp.next #{temp.next} @head.next #{@head.next}" - # @head.next = temp - # temp = temp.next - # puts "temp #{temp} @head #{@head} temp.next #{temp.next} @head.next #{@head.next}" - # end - # temp = @head - # puts "temp #{temp} @head #{@head}" + #going to need three pointers + prev = nil + current = @head + while current != nil + temp = current.next + current.next = prev + prev = current + current = temp + end + @head = prev end ## Advanced Exercises @@ -169,15 +165,21 @@ def reverse # Time Complexity: # Space Complexity def find_middle_value - raise NotImplementedError + length = self.length + return self.get_at_index(length / 2 - 1) 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 + # Space Complexity O(1) def find_nth_from_end(n) - raise NotImplementedError + length = self.length + if n > length + return nil + else + return self.get_at_index(length - n - 1) + end end # checks if the linked list has a cycle. A cycle exists if any node in the @@ -186,14 +188,15 @@ def find_nth_from_end(n) # Time Complexity: # Space Complexity def has_cycle + # does this just mean an infinite loop? raise NotImplementedError 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 if @head == nil return nil @@ -206,8 +209,8 @@ def get_first end # method that inserts a given value as a new last node in the linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity constant + 1? def add_last(value) puts value last = Node.new(value) @@ -224,8 +227,8 @@ def add_last(value) # 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) + # Space Complexity constant def get_last return nil if @head == nil val = @head.data