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 501c60fb..d074c721 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,213 @@ 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)
- 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: O(1)
+ # Space Complexity: O(1)
+ 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:
- # Space Complexity
+ # Time Complexity: O(n)
+ # Space Complexity: O(1)
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:
- # Space Complexity
+ # Time Complexity: O(n), where n is the length of the list
+ # Space Complexity: O(1)
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:
- # Space Complexity
+ # Time Complexity: O(n), where n is the length of the list
+ # Space Complexity: O(1)
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:
- # Space Complexity
+ # Time Complexity: O(n), where n is the length of the list
+ # Space Complexity: O(1)
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: 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
+ 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: O(n), where n is the length of the list
+ # Space Complexity: O(1)
+ 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:
- # Space Complexity
+ # Time Complexity: O(n)
+ # Space Complexity: O(1)
def get_at_index(index)
- raise NotImplementedError
+ 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?
+ current = current.next
+ count += 1
+ end
+ current.data
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
- end
- # method to delete the first node found with specified value
- # Time Complexity:
- # Space Complexity
- def delete(value)
- 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
- 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: O(n)
+ # Space Complexity: O(1)
+ 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 +233,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:
- # Space Complexity
+ # Time Complexity: O(n)
+ # Space Complexity: O(1)
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 +288,4 @@ def create_cycle
current.next = @head # make the last node link to first node
end
end
+