From 5ee65292e151dfa5e9f84ae4a7c741e4ae0afe9d Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Thu, 22 Aug 2019 18:05:40 -0700 Subject: [PATCH 1/2] passing tests for now --- lib/linked_list.rb | 207 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 160 insertions(+), 47 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..53ffcd85 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -8,6 +8,7 @@ def initialize(value, next_node = nil) @data = value @next = next_node end + end # Defines the singly linked list @@ -18,79 +19,198 @@ 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: + # Time Complexity: # Space Complexity def add_first(value) - raise NotImplementedError + new_node = Node.new(value, nil) + new_node.next = @head + @head = new_node + + return new_node.data end + # Additional Exercises + # returns the value in the first node + # returns nil if the list is empty + # Time Complexity: + # Space Complexity + def get_first + return nil if @head.nil? + + return @head.data + end + # method to find if the linked list contains a node with specified value # returns true if found, false otherwise - # Time Complexity: + # Time Complexity: # Space Complexity def search(value) - raise NotImplementedError + node = @head + return false if !node.next + return node if node.value == value + while (node = node.next) + return node if node.value == value + end end # method to return the max value in the linked list # returns the data value and not the node - # Time Complexity: + # Time Complexity: # Space Complexity def find_max - raise NotImplementedError + if @head == nil + return nil + end + current = @head + max = current.data + while current + 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 # returns the data value and not the node - # Time Complexity: + # Time Complexity: # Space Complexity def find_min - raise NotImplementedError + if @head == nil + return nil + end + current = @head + min = current.data + while current + if current.data < min + min = current.data + end + current = current.next + end + return min end # method that returns the length of the singly linked list - # Time Complexity: + # Time Complexity: # Space Complexity def length - raise NotImplementedError + current = @head + + return 0 if current.nil? + length = 0 + + until current.nil? + current = current.next + length += 1 + end + + return length end + # method that inserts a given value as a new last node in the linked list + # Time Complexity: + # Space Complexity + def add_last(value) + new_node = Node.new(value) + if @head + current = @head + until current.next.nil? + current = current.next + end + current.next = new_node + else + @head = new_node + 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 + node = @head + + return nil if node.nil? + + return node.data if !node.next + return node.data if !node.next while (node = node.next) + + 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: + # Time Complexity: # Space Complexity def get_at_index(index) - raise NotImplementedError + return nil if @head.nil? + + count = 0 + current = @head + while count != index && !current.next.nil? + current = current.next + count += 1 + end + current.data end # method to print all the values in the linked list - # Time Complexity: + # Time Complexity: # Space Complexity def visit raise NotImplementedError end - # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity - def delete(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: + # Time Complexity: # Space Complexity def reverse - raise NotImplementedError + return @head if @head.nil? || @head.next.nil? + current_node = @head + previous_node = nil + + + while current_node + temp = current_node.next + current_node.next = previous_node + previous_node = current_node + current_node = temp + end + @head = previous_node + + end + + # method to delete the first node found with specified value + # Time Complexity: + # Space Complexity + def delete(value) + current = @head + before = nil + while current + if current.data == value + if !before && !current.next + @head = nil + elsif !before + @head = current.next + else + before.next = current.next + end + return "deleted" + end + before = current + current = current.next + end + return nil end ## Advanced Exercises # returns the value at the middle element in the singly linked list - # Time Complexity: + # Time Complexity: # Space Complexity def find_middle_value raise NotImplementedError @@ -98,49 +218,41 @@ 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: + # Time Complexity: # Space Complexity def find_nth_from_end(n) - raise NotImplementedError + selected_index = length - n - 1 + + if n >= length + return nil + end + current = @head + i = 0 + while i < selected_index && current.next + current = current.next + i += 1 + end + return current.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: + # 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 - 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 - 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 - end # method to insert a new node with specific data value, assuming the linked # list is sorted in ascending order - # Time Complexity: + # Time Complexity: # Space Complexity def insert_ascending(value) raise NotImplementedError @@ -161,3 +273,4 @@ def create_cycle current.next = @head # make the last node link to first node end end + From ba058add59f72e151d5b07001c5844ff7971db35 Mon Sep 17 00:00:00 2001 From: Ariana Bray Date: Tue, 27 Aug 2019 21:29:09 -0700 Subject: [PATCH 2/2] added time and space complexity --- lib/.idea/.gitignore | 3 ++ lib/.idea/google-java-format.xml | 6 +++ lib/.idea/lib.iml | 9 +++++ lib/.idea/modules.xml | 8 ++++ lib/.idea/vcs.xml | 6 +++ lib/linked_list.rb | 69 +++++++++++++++++++------------- 6 files changed, 74 insertions(+), 27 deletions(-) create mode 100644 lib/.idea/.gitignore create mode 100644 lib/.idea/google-java-format.xml create mode 100644 lib/.idea/lib.iml create mode 100644 lib/.idea/modules.xml create mode 100644 lib/.idea/vcs.xml diff --git a/lib/.idea/.gitignore b/lib/.idea/.gitignore new file mode 100644 index 00000000..0e40fe8f --- /dev/null +++ b/lib/.idea/.gitignore @@ -0,0 +1,3 @@ + +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/lib/.idea/google-java-format.xml b/lib/.idea/google-java-format.xml new file mode 100644 index 00000000..2aa056da --- /dev/null +++ b/lib/.idea/google-java-format.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/lib/.idea/lib.iml b/lib/.idea/lib.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/lib/.idea/lib.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/lib/.idea/modules.xml b/lib/.idea/modules.xml new file mode 100644 index 00000000..b0c4ff69 --- /dev/null +++ b/lib/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/lib/.idea/vcs.xml b/lib/.idea/vcs.xml new file mode 100644 index 00000000..6c0b8635 --- /dev/null +++ b/lib/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 53ffcd85..d074c721 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -19,8 +19,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) (constant), because the length of the list doesn't matter + # Space Complexity: O(n), where n is the length of the list def add_first(value) new_node = Node.new(value, nil) new_node.next = @head @@ -32,8 +32,8 @@ def add_first(value) # 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? @@ -42,8 +42,8 @@ def get_first # 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 return false if !node.next @@ -55,8 +55,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 if @head == nil return nil @@ -74,8 +74,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 if @head == nil return nil @@ -93,8 +93,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 current = @head @@ -110,8 +110,9 @@ def length 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: unsure. O(1) because we're not creating a new data structure, we're just adding to + # an existing one? def add_last(value) new_node = Node.new(value) if @head @@ -127,8 +128,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), where n is the length of the list + # Space Complexity: O(1) def get_last node = @head @@ -141,11 +142,13 @@ def get_last # 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) return nil if @head.nil? + # I feel like I could do something like use the length method from before and compare the result to the index + count = 0 current = @head while count != index && !current.next.nil? @@ -156,18 +159,30 @@ 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(n) def visit - raise NotImplementedError + + visit_array = [] + if self.length == 0 + puts "empty" + else + node = @head + until node.nil? + visit_array << node.data + node = node.next + end + end + + return visit_array 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(1) def reverse return @head if @head.nil? || @head.next.nil? current_node = @head @@ -185,8 +200,8 @@ def reverse end # method to delete the first node found with specified value - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O(1) def delete(value) current = @head before = nil @@ -218,8 +233,8 @@ 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(1) def find_nth_from_end(n) selected_index = length - n - 1