Skip to content
Open
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
129 changes: 106 additions & 23 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,63 +18,146 @@ 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)
Comment on lines +21 to 23

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError

new_node = Node.new(value)
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(n)
def search(value)
Comment on lines +35 to 37

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1) because you're not creating new nodes here.

raise NotImplementedError
# raise NotImplementedError
current_node = @head

while current_node
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
def find_max
Comment on lines 53 to 55

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
if @head == nil
return nil
end

max_value = @head.data
current_node = @head

while current_node
if current_node.data > max_value
max_value = current_node.data
current_node = current_node.next
else
current_node = current_node.next
end
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: ?
def find_min
Comment on lines 77 to 81

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError

if @head == nil
return nil
end

min_value = @head.data
current_node = @head

while current_node
if current_node.data < min_value
min_value = current_node.data
current_node = current_node.next
else
current_node = current_node.next
end
end


return min_value
end


# 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
Comment on lines +108 to 110

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# 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: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def add_last(value)
Comment on lines +122 to 124

Choose a reason for hiding this comment

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

👍 , however the space complexity is O(1) because you're not creating new nodes.

raise NotImplementedError
# raise NotImplementedError
new_node = Node.new(value)

if @head == nil
@head = new_node
else
current_node = @head
until current_node.next == nil do
current_node = current_node.next
end
current_node.next = new_node
end
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
Comment on lines +140 to 142

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
length = 0
current_node = @head

while current_node
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)
# Space Complexity: O(n)
def get_at_index(index)
Comment on lines +159 to 161

Choose a reason for hiding this comment

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

Just noting this isn't done and the ones below.

raise NotImplementedError
end
Expand All @@ -87,16 +170,16 @@ def visit
end

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def delete(value)
raise NotImplementedError
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(n)
def reverse
raise NotImplementedError
end
Expand Down