From e028716791e10f7db853a2b1f006546d6ea9feae Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Wed, 28 Aug 2019 00:16:45 -0700 Subject: [PATCH 1/3] updated find min max methods --- lib/linked_list.rb | 95 +++++++++++++++++++++++++++++++++------- test/linked_list_test.rb | 5 +++ 2 files changed, 83 insertions(+), 17 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 501c60fb..38f360b4 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,42 +18,92 @@ 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(n) + # Space Complexity: O(1) def add_first(value) - raise NotImplementedError + if @head == nil + @head = Node.new(value, nil) + else + new_node = Node.new(value, nil) + new_node.next = @head + @head = new_node + 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 + # Time Complexity: O(n) + # Space Complexity: O(1) def search(value) - raise NotImplementedError + if @head == nil + return false + else + current = @head + until current == nil + if current.data == value + return true + else + return false + end + end + end end # 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 - raise NotImplementedError + if @head == nil + return nil + end + # p @head + max_val = @head.data + current = @head + until current == nil + if current.data != nil && current.data > max_val + max_val = current.data + + end + current = current.next + end + return max_val + 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_val = @head.data + current = @head + + until current == nil + if current.data != nil && current.data < min_val + min_val = current.data + end + current = current.next + end + return min_val + end # method that returns the length of the singly linked list - # Time Complexity: - # Space Complexity + # Time Complexity: O(n) + # Space Complexity: O(n) def length - raise NotImplementedError + current = @head + count = 0 + until current == nil + count += 1 + current = current.next + end + return count end # method that returns the value at a given index in the linked list @@ -120,14 +170,25 @@ def has_cycle # Time Complexity: # Space Complexity def get_first - raise NotImplementedError + if @head == nil + return nil + else + return @head.data + end 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 + second_to_last_node = @head + new_node = Node.new(value, nil) + until current == nil + prev = current + current = current.next + end + prev.next = new_node end # method that returns the value of the last node in the linked list diff --git a/test/linked_list_test.rb b/test/linked_list_test.rb index a7ee3769..626c09be 100644 --- a/test/linked_list_test.rb +++ b/test/linked_list_test.rb @@ -43,6 +43,8 @@ # Assert expect(@list.get_first).must_equal 3 + expect(@list.search(3)).must_equal true + expect(@list.search(99)).must_equal false end it 'will return `nil` for `getFirst` if the list is empty' do @@ -140,7 +142,10 @@ expect(@list.find_min).must_equal 0 count += 1 end + + @list.add_last(100) + @list.add_first(-12) # Act-Assert From 3819505385308e284079d25ae23d61ac3c8fae22 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Thu, 29 Aug 2019 00:38:58 -0700 Subject: [PATCH 2/3] added delete updated add_last functions --- lib/linked_list.rb | 84 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 17 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 38f360b4..d3bddee0 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -95,7 +95,7 @@ def find_min # method that returns the length of the singly linked list # Time Complexity: O(n) - # Space Complexity: O(n) + # Space Complexity: O(1) def length current = @head count = 0 @@ -109,24 +109,54 @@ 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) - raise NotImplementedError + current = @head + count = 0 + until current == nil + if count == index && current != nil + return current.data + end + count += 1 + current = current.next + end + if count < index + return nil + end end # method to print all the values in the linked list # Time Complexity: # Space Complexity def visit - raise NotImplementedError + current == @head + until current == nil + p current.data + current = current.next + end 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 + current = @head + if current == nil + return + end + if current.data == value + @head = current.next + return + end + until current == nil + previous_node = current + current = current.next + if current.data == value && current != nil + previous_node.next = current.next + return + end + end end # method to reverse the singly linked list @@ -140,10 +170,17 @@ def reverse ## 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 + slow = @head + fast = @head.next + + until fast == nil + slow = slow.next + fast = fast.next + end + return slow.data end # find the nth node from the end and return its value @@ -178,12 +215,16 @@ 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: O(1) def add_last(value) current = @head - second_to_last_node = @head + prev = @head new_node = Node.new(value, nil) + if current == nil + @head = new_node + return + end until current == nil prev = current current = current.next @@ -193,10 +234,19 @@ 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(1) def get_last - raise NotImplementedError + current = @head + second_to_last_node = @head + if current == nil + return nil + end + until current == nil + prev = current + current = current.next + end + return prev.data end # method to insert a new node with specific data value, assuming the linked From a39cd1a7bedb315520dc93b4b69cd2f6335ef086 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Tue, 3 Sep 2019 23:26:04 -0700 Subject: [PATCH 3/3] added reverse method --- lib/linked_list.rb | 48 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index d3bddee0..3413029f 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -127,8 +127,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 until current == nil @@ -161,10 +161,19 @@ 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) + # Space Complexity: O(1) def reverse - raise NotImplementedError + current = @head + previous = nil + temp = nil + until current == nil + temp = current.next + current.next = previous + previous = current + current = temp + end + @head = previous end @@ -188,7 +197,34 @@ def find_middle_value # Time Complexity: # Space Complexity def find_nth_from_end(n) - raise NotImplementedError + + counter = 0 + current = @head + trailing_current = nil + if current == nil + return nil + end + if n > length + return nil + end + + while current != nil + if counter == n + 1 + trailing_current = @head + end + if trailing_current != nil + trailing_current = trailing_current.next + end + current = current.next + counter += 1 + end + if counter == n + 1 + return @head.data + end + if trailing_current == nil + return nil + end + return trailing_current.data end # checks if the linked list has a cycle. A cycle exists if any node in the