From 5af05a59044a40133b1b85b9fd3f04141e4aae9d Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 26 Aug 2019 21:21:34 -0700 Subject: [PATCH 1/2] implemented methods (except one), tests are passing --- lib/linked_list.rb | 140 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 125 insertions(+), 15 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..9e249195 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,7 +21,7 @@ def initialize # Time Complexity: # Space Complexity def add_first(value) - raise NotImplementedError + @head = Node.new(value, @head) end # method to find if the linked list contains a node with specified value @@ -29,7 +29,16 @@ def add_first(value) # Time Complexity: # Space Complexity def search(value) - raise NotImplementedError + return false if @head = nil + + current = @head + until current == nil + if current.data == value + return true + end + current = current.next + end + return false end # method to return the max value in the linked list @@ -37,7 +46,19 @@ def search(value) # Time Complexity: # Space Complexity def find_max - raise NotImplementedError + return nil if @head == nil + max = @head.data + current = @head + + while current != nil + if current.data > max + max = current.data + + end + current = current.next + end + + return max end # method to return the min value in the linked list @@ -45,7 +66,18 @@ def find_max # Time Complexity: # Space Complexity def find_min - raise NotImplementedError + return nil if @head == nil + min = @head.data + current = @head + + while current != nil + if current.data < min + min = current.data + end + current = current.next + end + + return min end @@ -53,7 +85,13 @@ def find_min # Time Complexity: # Space Complexity def length - raise NotImplementedError + count = 0 + current = @head + while current != nil + count += 1 + current = current.next + end + return count end # method that returns the value at a given index in the linked list @@ -62,21 +100,49 @@ def length # Time Complexity: # Space Complexity def get_at_index(index) - raise NotImplementedError + return nil if @head == nil + return nil if (self.length - 1) < index + + current = @head + + index.times do + current = current.next + end + + return current.data end # method to print all the values in the linked list # Time Complexity: # Space Complexity def visit - raise NotImplementedError + current = @head + + while current != nil + puts current.data + end end # method to delete the first node found with specified value # Time Complexity: # Space Complexity def delete(value) - raise NotImplementedError + return nil if @head == nil + + if @head.data == value + @head = @head.next + return + end + + current = @head + + while current.next != nil + if current.next.data == value + current.next = current.next.next + return + end + current = current.next + end end # method to reverse the singly linked list @@ -84,7 +150,17 @@ def delete(value) # Time Complexity: # Space Complexity def reverse - raise NotImplementedError + current = @head + previous = nil + + while current != nil + temp = current.next + current.next = previous + previous = current + current = temp + end + + @head = previous end @@ -93,7 +169,7 @@ def reverse # Time Complexity: # Space Complexity def find_middle_value - raise NotImplementedError + return self.get_at_index(self.length / 2) end # find the nth node from the end and return its value @@ -101,7 +177,15 @@ def find_middle_value # Time Complexity: # Space Complexity def find_nth_from_end(n) - raise NotImplementedError + largest_index = self.length - 1 + return nil if n > largest_index + count = 0 + current = @head + until (largest_index - count) == n + count +=1 + current = current.next + end + return current.data end # checks if the linked list has a cycle. A cycle exists if any node in the @@ -120,14 +204,24 @@ def has_cycle # Time Complexity: # Space Complexity def get_first - raise NotImplementedError + 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: # Space Complexity def add_last(value) - raise NotImplementedError + if @head == nil + @head = Node.new(value, nil) + else + current = @head + while current.next != nil + current = current.next + end + + current.next = Node.new(value, nil) + end end # method that returns the value of the last node in the linked list @@ -135,7 +229,15 @@ def add_last(value) # Time Complexity: # Space Complexity def get_last - raise NotImplementedError + puts "get_last" + return nil if @head == nil + current = @head + while current.next != nil + current = current.next + end + + puts "end of get_last" + return current.data end # method to insert a new node with specific data value, assuming the linked @@ -143,7 +245,15 @@ def get_last # Time Complexity: # Space Complexity def insert_ascending(value) - raise NotImplementedError + add_first(value) if @head == nil + + current = @head + while current.next != nil + if current.next.data > value + new_node = Node.new(value, current.next) + current.next = new_node + end + end end # Helper method for tests From e60621e990e3fd28020a955edd8bddaa25312420 Mon Sep 17 00:00:00 2001 From: Erica Norris Date: Mon, 26 Aug 2019 21:31:36 -0700 Subject: [PATCH 2/2] added time and space complexities, got rid of some puts statements --- lib/linked_list.rb | 58 ++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 9e249195..8ca6041e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -26,8 +26,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), where n is the length of the list + # Space Complexity: O(1) def search(value) return false if @head = 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), where n is the length of the list + # Space Complexity: O(1) def find_max return nil if @head == nil max = @head.data @@ -63,8 +63,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), where n is the length of the list + # Space Complexity: O(1) def find_min return nil if @head == nil min = @head.data @@ -82,8 +82,8 @@ def find_min # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1) def length count = 0 current = @head @@ -97,8 +97,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), where n is the size of the index + # Space Complexity: O(1) def get_at_index(index) return nil if @head == nil return nil if (self.length - 1) < index @@ -113,8 +113,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), where n is the length of the list + # Space Complexity: ?? I'm not sure if puts counts as using memory. If it does, then O(n), where n is the length of the list. Otherwise O(1) def visit current = @head @@ -124,8 +124,8 @@ def visit end # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1) def delete(value) return nil if @head == nil @@ -147,8 +147,8 @@ def delete(value) # 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), where n is the length of the list + # Space Complexity: O(1) def reverse current = @head previous = nil @@ -166,16 +166,16 @@ def reverse ## Advanced Exercises # returns the value at the middle element in the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1) def find_middle_value return self.get_at_index(self.length / 2) 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), where n is the length of the list + # Space Complexity: O(1) def find_nth_from_end(n) largest_index = self.length - 1 return nil if n > largest_index @@ -201,16 +201,16 @@ def has_cycle # 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 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: - # Space Complexity + # Time Complexity: O(n), where n is the length of the list + # Space Complexity: O(1) def add_last(value) if @head == nil @head = Node.new(value, nil) @@ -226,24 +226,22 @@ 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), where n is the length of the list + # Space Complexity: O(1) def get_last - puts "get_last" return nil if @head == nil current = @head while current.next != nil current = current.next end - puts "end of get_last" return current.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), where n is the length of the list + # Space Complexity: O(1) def insert_ascending(value) add_first(value) if @head == nil