From 6563b39c1d2aff6bb5fa3a4d6d15ee23f5eb27a9 Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 15:36:14 -0400 Subject: [PATCH 01/10] completed basic, did some advnaced --- lib/linked_list.rb | 326 +++++++++++++++++++++++++++++---------------- 1 file changed, 212 insertions(+), 114 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..54bcc347 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -12,150 +12,248 @@ 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 + # 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: O(1) + # Space Complexity: O(1) + def add_first(value) + if @head + second = @head + @head = Node.new(value, second) + else + @head = Node.new(value) end + 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: O(n) + # Space Complexity: O(1) + def search(value) + if @head == nil + return false + end + return search_help(value) != nil + end + def search_help(value) + current = @head + while current != nil + if current.data == value + return current + else + current = current.next + end end + return nil + end - # method to return the max value in the linked list - # returns the data value and not the node - def find_max - raise NotImplementedError + # method to return the max value in the linked list + # returns the data value and not the node + def find_max + if @head == nil + return nil end + 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 - # returns the data value and not the node - # Time Complexity: ? - # Space Complexity: ? - def find_min - raise NotImplementedError + # method to return the min value in the linked list + # returns the data value and not the node + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_min + if @head == nil + return nil end + min = @head.data + current = @head + while current != nil + 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: ? - # Space Complexity: ? - def length - raise NotImplementedError + # method that returns the length of the singly linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + def length + if @head == nil + return 0 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 + current = @head + length = 1 + while current != nil + length += 1 + current = current.next end + return length + end - # method to print all the values in the linked list - # Time Complexity: ? - # Space Complexity: ? - def visit - 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: O(n) + # Space Complexity: O(1) + def get_at_index(index) + return get_node_at_index.data + end + def get_node_at_index(index) + length = this.length + if length < index + return nil end - - # method to delete the first node found with specified value - # Time Complexity: ? - # Space Complexity: ? - def delete(value) - raise NotImplementedError + current = @head + (0..length-1).each do |i| + if i == index + return current + 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: ? - def reverse - raise NotImplementedError + # method to print all the values in the linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + 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: O(n) + # Space Complexity: O(1) + def delete(value) + target = search_help(value) + after_target = target.next + target.next = nil + before_target_index = this.length - 2 + before_target = get_node_at_index(before_target_index) + before_target.next = after_target + 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 + # method to reverse the singly linked list + # note: the nodes should be moved and not just the values in the nodes + # Time Complexity: O(n) + # Space Complexity: O(n) + def reverse + new_list = LinkedList.new + current = @head + while current != nil + new_list.add_first(current) + current = current.next end + this = new_list + 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 + ## Advanced Exercises + # returns the value at the middle element in the singly linked list + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_middle_value + length = this.length + middle_index = length/2 + return get_at_index(middle_index) + end + # find the nth node from the end and return its value + # assume indexing starts at 0 while counting to n + # Time Complexity: O(n) + # Space Complexity: O(1) + def find_nth_from_end(n) + length = this.length + nth_from_end_index = length - 1 - n + return get_at_index(nth_from_end_index) + 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 + # 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 + current = @head + seen_nodes = [] + # while current != nil || has_node?(current) + # current = current.next + # if current == nil + # return false + # end + # if has_node + # end + end + def has_node?(array, node) + array.each do |current_node| + if current_node == node + return true + end end + return false + 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 + # 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 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 + # 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 - # 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 cfd57d4bf1c565d6e86669b253e8419692d23363 Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 15:42:25 -0400 Subject: [PATCH 02/10] fixed bug in length --- lib/linked_list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 54bcc347..471bc5c4 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -96,7 +96,7 @@ def length return 0 end current = @head - length = 1 + length = 0 while current != nil length += 1 current = current.next From 6d49e35ce2346a5e6fd7c89c819457c71662b1f4 Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 15:43:38 -0400 Subject: [PATCH 03/10] fixed bug in get_index --- lib/linked_list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 471bc5c4..2fcfa8f8 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -110,7 +110,7 @@ def length # Time Complexity: O(n) # Space Complexity: O(1) def get_at_index(index) - return get_node_at_index.data + return get_node_at_index(index).data end def get_node_at_index(index) length = this.length From 3410ad5854a2c82408b886b316891affbe749052 Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 15:46:23 -0400 Subject: [PATCH 04/10] replaced this with right ruby keyword, self --- lib/linked_list.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 2fcfa8f8..b813f09e 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -13,7 +13,7 @@ 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 + @head = nil # keep the head private. Not accessible outside self class end # method to add a new node with the specific data value in the linked list @@ -113,7 +113,7 @@ def get_at_index(index) return get_node_at_index(index).data end def get_node_at_index(index) - length = this.length + length = self.length if length < index return nil end @@ -144,7 +144,7 @@ def delete(value) target = search_help(value) after_target = target.next target.next = nil - before_target_index = this.length - 2 + before_target_index = self.length - 2 before_target = get_node_at_index(before_target_index) before_target.next = after_target end @@ -160,7 +160,7 @@ def reverse new_list.add_first(current) current = current.next end - this = new_list + self = new_list end @@ -169,7 +169,7 @@ def reverse # Time Complexity: O(n) # Space Complexity: O(1) def find_middle_value - length = this.length + length = self.length middle_index = length/2 return get_at_index(middle_index) end @@ -179,7 +179,7 @@ def find_middle_value # Time Complexity: O(n) # Space Complexity: O(1) def find_nth_from_end(n) - length = this.length + length = self.length nth_from_end_index = length - 1 - n return get_at_index(nth_from_end_index) end From 1b111246ec68bf755a12fea03e9f9574ac451c43 Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 15:50:11 -0400 Subject: [PATCH 05/10] can't re-assign self, so changed reverse implementation, first attempt --- lib/linked_list.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index b813f09e..4f125b31 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -160,7 +160,9 @@ def reverse new_list.add_first(current) current = current.next end - self = new_list + @head.next = nil + @head.data = nil + @head = new_list end From e9ab1a665009a82f52f5ef10c743488919ac9bb3 Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 19:01:56 -0400 Subject: [PATCH 06/10] added get_first --- lib/linked_list.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 4f125b31..b58a3317 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -218,7 +218,11 @@ def has_node?(array, node) # Time Complexity: ? # Space Complexity: ? def get_first - raise NotImplementedError + if @head + return @head + else + return nil + end end # method that inserts a given value as a new last node in the linked list From 0fdb1b71cb47c4ea87f89054b40aac26460934c6 Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 19:07:15 -0400 Subject: [PATCH 07/10] fixed get_at_index for nil case --- lib/linked_list.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index b58a3317..2e741ad3 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -110,7 +110,12 @@ def length # Time Complexity: O(n) # Space Complexity: O(1) def get_at_index(index) - return get_node_at_index(index).data + node = get_node_at_index(index) + if node + return node.data + else + return nil + end end def get_node_at_index(index) length = self.length From 27fb9a65d90a862249e1c1468bef1d0fada59f1b Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 19:09:39 -0400 Subject: [PATCH 08/10] added additional nil return case for get_node_at_index --- lib/linked_list.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 2e741ad3..6e0466da 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -123,12 +123,13 @@ def get_node_at_index(index) return nil end current = @head - (0..length-1).each do |i| + (0..(length-1)).each do |i| if i == index return current end current = current.next end + return nil end # method to print all the values in the linked list From f8ab2f64c10d5f759780f3b592d767102bfd829d Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 19:13:04 -0400 Subject: [PATCH 09/10] changed list reassignment statement in #reverse to actually work, instead of tring to assign a list to a node --- lib/linked_list.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 6e0466da..c1481778 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -167,8 +167,7 @@ def reverse current = current.next end @head.next = nil - @head.data = nil - @head = new_list + @head = new_list.get_first end From 0d5884d1bdd3f21b3685f09b76346194ff105bd3 Mon Sep 17 00:00:00 2001 From: saintmedusa Date: Thu, 27 Aug 2020 19:17:39 -0400 Subject: [PATCH 10/10] moved my helper methods to private --- lib/linked_list.rb | 55 +++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index c1481778..4233234c 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -39,17 +39,6 @@ def search(value) end return search_help(value) != nil end - def search_help(value) - current = @head - while current != nil - if current.data == value - return current - else - current = current.next - end - end - return nil - end # method to return the max value in the linked list # returns the data value and not the node @@ -117,20 +106,6 @@ def get_at_index(index) return nil end end - def get_node_at_index(index) - length = self.length - if length < index - return nil - end - current = @head - (0..(length-1)).each do |i| - if i == index - return current - end - current = current.next - end - return nil - end # method to print all the values in the linked list # Time Complexity: O(n) @@ -267,4 +242,34 @@ def create_cycle current.next = @head # make the last node link to first node end + + private + + def get_node_at_index(index) + length = self.length + if length < index + return nil + end + current = @head + (0..(length-1)).each do |i| + if i == index + return current + end + current = current.next + end + return nil + end + + def search_help(value) + current = @head + while current != nil + if current.data == value + return current + else + current = current.next + end + end + return nil + end + end