Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Earth - Sophie #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
108 changes: 88 additions & 20 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,133 @@ 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

# if head is nil
return @head = Node.new(value) if @head.nil?

# if head contains data
new_node = Node.new(value)
new_node.next = @head
@head = new_node
return @head
Comment on lines +25 to +32

Choose a reason for hiding this comment

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

Suggested change
# if head is nil
return @head = Node.new(value) if @head.nil?
# if head contains data
new_node = Node.new(value)
new_node.next = @head
@head = new_node
return @head
@head = Node.new(value, @head)

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)
Comment on lines +37 to 39

Choose a reason for hiding this comment

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

👍

raise NotImplementedError

current_node = @head

while current_node
return true if current_node.data == value
current_node = current_node.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
Comment on lines +52 to 54

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 = 0

while current_node
if current_node.data > max
max = current_node.data
current_node = current_node.next
else
current_node = current_node.next
end
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
Comment on lines +73 to 75

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 = current_node.data

while current_node
if current_node.data < min
min = current_node.data
current_node = current_node.next
else
current_node = current_node.next
end
end
return min
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 +95 to 97

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return @head ? @head.data : nil
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
def add_last(value)
Comment on lines 102 to 104

Choose a reason for hiding this comment

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

👍 Time complexity O(n) and space complexity O(1)

raise NotImplementedError
last_node = Node.new(value)

if @head.nil?
@head = last_node
else
current_node = @head
until current_node.next.nil?
current_node = current_node.next
end
current_node.next = last_node
end
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
Comment on lines +119 to 121

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current_node = @head
count = 0

while current_node
count += 1
current_node = current_node.next
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)
Comment on lines +135 to 137

Choose a reason for hiding this comment

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

👍

raise NotImplementedError

current_node = @head
count = 0

while current_node
return current_node.data if count == index
current_node = current_node.next
count += 1
end
return nil
end

# method to print all the values in the linked list
Expand Down