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
263 changes: 217 additions & 46 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,128 +18,299 @@ 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), will take same amount of time regardless of the length of the list
# Does not have to perform any additional operations to move the existing nodes
# Space Complexity: O(1), needs space for one new node
# Does not need any additional space for existing nodes as they are unaffected
# Would be O(n) where n is the size of the value being saved to the new node,
# but if all nodes are integers and therefore comparable sizes, n is constant
def add_first(value)
Comment on lines +21 to 27

Choose a reason for hiding this comment

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

👍

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), in the worst case scenario needs to check every single
# node in a linked list of n length
# Space Complexity: O(n), just needs to save one node to a variable
def search(value)
Comment on lines +35 to 38

Choose a reason for hiding this comment

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

👍 , but the space complexity for the iterative solution is O(1)

raise NotImplementedError
current = @head
while current
return true if current.data == value
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). Will need to check every node in linked list of n length
# Space Complexity: O(1). Will need two running variables regardless of length of list
def find_max
Comment on lines +50 to 52

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 = @head
max = @head.data

while current
max = current.data > max ? current.data : max
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). Will need to check every node in linked list of n length
# Space Complexity: O(1). Will need two running variables regardless of length of list
def find_min
Comment on lines +67 to 69

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 = @head
max = @head.data

while current
max = current.data < max ? current.data : max
current = current.next
end

return max
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), checking the head node takes one operation
# Space Complexity: O(1) assuming all nodes have comparable data sizes, same size return value regardless
def get_first
Comment on lines +86 to 88

Choose a reason for hiding this comment

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

👍

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

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), will need to iterate over entire linked list of length n
# Space Complexity: O(1)
def add_last(value)
Comment on lines +93 to 95

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?
@head = Node.new(value)
else
current = @head
while current.next
current = current.next
end
new_node = Node.new(value)
current.next = new_node
end
end

# method that returns the length of the singly linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), will need to iterate over entire linked list of length n
# Space Complexity: O(1)
def length
Comment on lines +109 to 111

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

count = 1
current = @head

while current.next
count += 1
current = current.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), will increase linearly with the size of the index being sought,
# will potentially have to iterate through entire linked list if item sought is last or does not exist
# Space Complexity: O(1)
def get_at_index(index)
Comment on lines +128 to 131

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?

counter = 0
current = @head

until counter == index
return nil if current.nil?
counter += 1
current = current.next
end

return current.data
end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), will have to check each node in linked list of length n
# Space Complexity: O(n), will have to store values of n number of nodes in linked list of length n
def visit
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.

👍 , but the description says the method prints the values in the list.

raise NotImplementedError
values = []
current = @head

while current
values.push(current.data)
current = current.next
end

return values
end

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), has to iterate over linked list of length n to find the node to delete
# Space Complexity: O(1)
def delete(value)
Comment on lines +162 to 164

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?
@head = @head.next if @head.data == value

current = @head
until current.next.nil? || current.next.data == value
current = current.next
end

to_delete = current.next
if to_delete
current.next = to_delete.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), n = length of linked list
# Space Complexity: O(1)
def reverse
Comment on lines +181 to 183

Choose a reason for hiding this comment

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

👍 , but I wouldn't use variable names like p1, p2 and p3.

raise NotImplementedError
# assign three pointers: one to head, one to head.next, one to head.next.next
p1 = @head
p2 = p1.next
p3 = p2.next

# tell head to point to nil
p1.next = nil

# point p2 to p1 and move all pointers forward
while p3
p2.next = p1
p1 = p2
p2 = p3
p3 = p3.next
end

# last link
p2.next = p1

# once p3 is nil, p2 is pointing at the (former) tail, now head
@head = p2
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: ?
# Time Complexity: O(n), n = length of linked list
# Space Complexity: O(1)
def get_last
Comment on lines +209 to 211

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 = @head
while current.next
current = current.next
end

return current.data
end

## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), n = length of linked list
# Space Complexity: O(1)
def find_middle_value
Comment on lines +224 to 226

Choose a reason for hiding this comment

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

👍 , good use of a fast and slow pointer

raise NotImplementedError
# two pointers, one moving twice every iteration, the other moving once
slow = @head
fast = @head

while fast.next
fast = fast.next
slow = slow.next
if fast.next
fast = fast.next
else
return slow.data
end
end

return slow.data
end

# 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), n = length of linked list
# Space Complexity: O(1)
def find_nth_from_end(n)
Comment on lines +246 to 248

Choose a reason for hiding this comment

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

I like the lead pointer.

raise NotImplementedError
count = 0
lead = @head
tail = @head

until count == n
return nil if lead.nil?
lead = lead.next
count += 1
end

return nil if lead.nil?

until lead.next.nil?
tail = tail.next
lead = lead.next
end

return tail.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: ?
# Space Complexity: ?
# Time Complexity: O(n), n = length of linked list
# Space Complexity: O(n), n = number of nodes (length of linked list)
def has_cycle
Comment on lines +272 to 274

Choose a reason for hiding this comment

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

That is one solution. However since you are using .include? this solution is O(n^2) in time complexity.

You can do it in O(n) with a fast and slow pointer.

raise NotImplementedError
already_seen = []
current = @head

while current
return true if already_seen.include? current

already_seen.push(current)
current = current.next
end

return false
end

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def insert_ascending(value)
Comment on lines +290 to 292

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? || value < @head.data
self.add_first(value)
return
end

current = @head

while current.next
if current.data <= value && value <= current.next.data
new_node = Node.new(value)
new_node.next = current.next
current.next = new_node
return
else
current = current.next
end
end

if value >= current.data
current.next = Node.new(value)
end
end

# Helper method for tests
Expand Down
Loading