From ed8440538db0e3fd5e34989c88e8d4ca57080b08 Mon Sep 17 00:00:00 2001 From: thenora Date: Mon, 24 Aug 2020 21:56:26 -0700 Subject: [PATCH 1/5] did basics and advanced exercises --- lib/linked_list.rb | 220 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 190 insertions(+), 30 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0de1ee00..b568119d 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,80 +18,208 @@ 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: O(1) 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 # returns true if found, false otherwise - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def search(value) - raise NotImplementedError + if head.nil? + return false + end + + current = @head + + while current != nil + if current.data == value + return true + end + current = current.next + end + end # method to return the max value in the linked list # returns the data value and not the node + # Time Complexity: O(n) + # Space Complexity: O(1) def find_max - raise NotImplementedError + if @head.nil? + return nil + end + + max = @head.data + current = @head + while current != nil + if max < current.data + 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def find_min - raise NotImplementedError + if @head.nil? + return nil + end + + min = @head.data + current = @head + while current != nil + if min > current.data + 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: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def length - raise NotImplementedError + if @head.nil? + return 0 + end + + length = 0 + current = @head + while current != nil + length += 1 + current = current.next + end + + return length 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) + # Space Complexity: o(1) def get_at_index(index) - raise NotImplementedError + if @head.nil? + return 0 + end + + count = 0 + current = @head + while current != nil + if count == index + return current.data + end + count += 1 + current = current.next + end + + return nil 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 + if @head.nil? + return + end + + current = @head + while current != nil + p current.data + current = current.next + end + + return 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) - raise NotImplementedError + if @head.nil? + return + end + + if @head.data == value + @head = @head.next + return + end + + previous = nil + current = @head + while current != nil + if current.data == value + previous.next = current.next_node + current = nil + return + end + + previous = current + current = current.next + end + + return 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 - raise NotImplementedError + if @head.nil? + return + end + + current = @head + previous = nil + + while current != nil + temp = current.next + current.next = previous + previous = current + current = temp + end + + @head = previous + return end ## Advanced Exercises # returns the value at the middle element in the singly linked list - # Time Complexity: ? - # Space Complexity: ? + # Time Complexity: O(n) + # Space Complexity: O(1) def find_middle_value - raise NotImplementedError + if @head.nil? + return + end + + slow = @head + fast = @head.next + while fast != nil + slow = slow.next + fast = fast.next + if fast != nil + fast = fast.next + end + end + + return slow.data end # find the nth node from the end and return its value @@ -99,7 +227,22 @@ def find_middle_value # Time Complexity: ? # Space Complexity: ? def find_nth_from_end(n) - raise NotImplementedError + if @head.nil? + return + end + + back = @head + front = @head + count = 1 + while front != nil + front = front.next + count += 1 + if front > n + back = back.next + end + end + + return back.data end # checks if the linked list has a cycle. A cycle exists if any node in the @@ -108,7 +251,24 @@ def find_nth_from_end(n) # Time Complexity: ? # Space Complexity: ? def has_cycle - raise NotImplementedError + if @head.nil? + return + end + + slow = @head + fast = @head.next + while fast != nil + slow = slow.next + fast = fast.next + if fast != nil + fast = fast.next + end + if fast == slow + return true + end + end + + return false end From 07cc05429f4055f3daa0080e6ed34973d87bd276 Mon Sep 17 00:00:00 2001 From: thenora Date: Mon, 24 Aug 2020 22:02:35 -0700 Subject: [PATCH 2/5] fixed for tests --- lib/linked_list.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index b568119d..0fd58fd5 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,6 +21,7 @@ def initialize # Time Complexity: O(1) # Space Complexity: O(1) def add_first(value) + return nil if @head.nil? @head = Node.new(value, @head) end @@ -29,7 +30,7 @@ def add_first(value) # Time Complexity: O(n) # Space Complexity: O(1) def search(value) - if head.nil? + if @head.nil? return false end @@ -114,7 +115,7 @@ def length # Space Complexity: o(1) def get_at_index(index) if @head.nil? - return 0 + return nil end count = 0 @@ -164,7 +165,7 @@ def delete(value) current = @head while current != nil if current.data == value - previous.next = current.next_node + previous.next = current.next current = nil return end From 5d88a8c53af8517a93de2a3bdd3114ca5cd3e685 Mon Sep 17 00:00:00 2001 From: thenora Date: Mon, 24 Aug 2020 22:09:16 -0700 Subject: [PATCH 3/5] . --- lib/linked_list.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 0fd58fd5..d32078ad 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -21,7 +21,6 @@ def initialize # Time Complexity: O(1) # Space Complexity: O(1) def add_first(value) - return nil if @head.nil? @head = Node.new(value, @head) end From 5521f7fa267b70f76c146fdb5d86e0aab6ecf288 Mon Sep 17 00:00:00 2001 From: thenora Date: Mon, 24 Aug 2020 22:15:57 -0700 Subject: [PATCH 4/5] added some additional --- lib/linked_list.rb | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index d32078ad..462d6306 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -42,6 +42,7 @@ def search(value) current = current.next end + return false end # method to return the max value in the linked list @@ -278,14 +279,25 @@ def has_cycle # Time Complexity: ? # Space Complexity: ? def get_first - raise NotImplementedError + return @head.data if @head != 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 + + current = @head + newNode = Node.new(value, nil) + + if current.nil? + @head = newNode + else + until current.next.nil? + current = current.next + end + current.next = newNode + end end # method that returns the value of the last node in the linked list @@ -293,7 +305,15 @@ def add_last(value) # Time Complexity: ? # Space Complexity: ? def get_last - raise NotImplementedError + if @head.nil? + return nil + else + current = @head + until current.next.nil? + current = current.next + end + end + return current.data end # method to insert a new node with specific data value, assuming the linked From 3d34d1ea660a27fac62bb2a756ed8724c04899d4 Mon Sep 17 00:00:00 2001 From: thenora Date: Mon, 24 Aug 2020 22:19:26 -0700 Subject: [PATCH 5/5] added space-time --- lib/linked_list.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 462d6306..4f2f4ecb 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -225,8 +225,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) if @head.nil? return @@ -276,15 +276,15 @@ 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 @head.data if @head != nil 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: O(1) def add_last(value) current = @head @@ -302,8 +302,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: O(n) def get_last if @head.nil? return nil