Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
171 changes: 143 additions & 28 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# Defines a node in the singly linked list
class Node
attr_reader :data # allow external entities to read value but not write
attr_accessor :next # allow external entities to read or write next node
attr_accessor :next, :previous, :data # allow external entities to read or write next node

def initialize(value, next_node = nil)
def initialize(value, next_node = nil, prev_node = nil)
@data = value
@next = next_node
@previous = prev_node
end
end

Expand All @@ -18,71 +19,184 @@ 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), since it will always have direct access to the front of the list
# Space Complexity: O(1)
def add_first(value)
Comment on lines +22 to 24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
if @head == nil
newNode = Node.new(value)
@head = newNode
return
end

current_head = @head
newNode = Node.new(value, next_node = current_head)
current_head.previous = newNode
@head = newNode
return
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), n being up to the length of the linked list
# Space Complexity: O(1)
def search(value)
Comment on lines +40 to 42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return false if @head == nil
return true if @head.data == value

current_node = @head
until current_node == nil
if current_node.data == value
return true
else
current_node = current_node.next
end
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), n being up to the length of the linked list
# Space Complexity: O(1)
def find_max
Comment on lines +60 to 62

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if @head == nil

current_node = @head
max_value = current_node.data
until current_node == nil
if current_node.data > max_value
max_value = current_node.data
end
current_node = current_node.next
end

return max_value
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), n being up to the length of the linked list
# Space Complexity: O(1)
def find_min
Comment on lines +79 to 81

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if @head == nil

current_node = @head
min_value = current_node.data
until current_node == nil
if current_node.data < min_value
min_value = current_node.data
end
current_node = current_node.next
end

return min_value
end


# method that returns the length of the singly linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), n being the length of the linked list
# Space Complexity: O(1)
def length
Comment on lines +98 to 100

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return 0 if @head == nil

current_node = @head
length = 0
until current_node == nil
length += 1
current_node = current_node.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), n being the index to get the value at
# Space Complexity: O(1)
def get_at_index(index)
Comment on lines +116 to 118

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if @head == nil

current_node = @head
current_index = 0
until current_node == nil
if current_index == index
return current_node.data
end
current_index += 1
current_node = current_node.next
end

return nil
end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), n being the amount of nodes
# Space Complexity: O(1)
def visit

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠ it's not printing anything.

raise NotImplementedError
return nil if @head == nil

current_node = @head
until current_node == nil
current_node = current_node.next
end
end

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), n being up to the the amount of nodes in the list
# Space Complexity: O(1)
def delete(value)
Comment on lines +147 to 149

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little overly complicated, but it works.

raise NotImplementedError
return nil if @head == nil

current_node = @head
previous_node = nil
next_node = current_node.next
until current_node == nil
if current_node.data == value
# if first node is being deleted
if previous_node == nil
next_node.previous = nil
@head = next_node
return
end

# if last node is deleted
if next_node == nil
previous_node.next = nil
return
end

next_node.previous = previous_node
previous_node.next = next_node
return
end
current_node = current_node.next
next_node = current_node.next
previous_node = current_node.previous
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), n being the number of nodes in the list
# Space Complexity: O(1)
def reverse
Comment on lines +182 to 184

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return if @head == nil

length = self.length
current_node = @head

until length == 0
next_node = current_node.next
current_node.next = current_node.previous
current_node.previous = next_node
current_node = next_node if next_node != nil
length -= 1
if next_node == nil
@head = current_node
end
end
end


Expand Down Expand Up @@ -118,7 +232,8 @@ def has_cycle
# Time Complexity: ?
# Space Complexity: ?
def get_first
Comment on lines 232 to 234

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError
return nil if @head == nil
return @head.data
end

# method that inserts a given value as a new last node in the linked list
Expand Down