From 000a66d8be2843bdc0b9718a48c3c42d45a969ce Mon Sep 17 00:00:00 2001 From: Hanh Solo Date: Sun, 18 Apr 2021 21:33:02 -0700 Subject: [PATCH] Linkedlist submission --- lib/linked_list.rb | 183 +++++++++++++++++++++++++++++++++------------ 1 file changed, 136 insertions(+), 47 deletions(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 5e173ebc..d9efd826 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -18,97 +18,160 @@ 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 + new_node = Node.new(value, nil) + new_node.next = @head + @head = new_node 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 + current = @head + + while 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 # returns the data value and not the node + # Time Complexity: O(n) + # Space Complexity: O(1) def find_max - raise NotImplementedError + return if @head == nil + + current = @head + max = current.data + + while current.next != nil + if max < current.next.data + max = current.next.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 - end + return if @head == nil + current = @head + min = current.data - # 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 + while current.next != nil + if current.next.data < min + min = current.next.data + end + current = current.next + 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 + 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 + current = @head + count = 0 + while current !=nil + current = current.next + count += 1 + end + return count 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 + current = @head + count = 0 + while current !=nil + if index == count + return current.data + end + current = current.next + count += 1 + 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(1) def visit - raise NotImplementedError + return if @head == nil + current = @head + + while current.next !=nil + current = current.next + print current.data + 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 + return 0 if @head == nil + + current = @head + if current.data == value + @head = current.next + return + end + + 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 # 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 - end + return if @head == nil + current = @head + previous = nil + + while !current.nil? + next_node = current.next + current.next = previous + previous = current + current = next_node + end + + @head = previous - # 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 - + + ## Advanced Exercises # returns the value at the middle element in the singly linked list # Time Complexity: ? @@ -134,6 +197,32 @@ 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 + return if @head == nil + return @head.value + 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: ?