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
342 changes: 229 additions & 113 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

require "pry"
# Defines a node in the singly linked list
class Node
attr_reader :data # allow external entities to read value but not write
Expand All @@ -12,150 +12,266 @@ def initialize(value, next_node = nil)

# Defines the singly linked list
class LinkedList
def initialize
@head = nil # keep the head private. Not accessible outside this class
end
def initialize
@head = nil # keep the head private. Not accessible outside this class
end

# 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: ?
def add_first(value)
raise NotImplementedError
end
# 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: O(1)
# Space Complexity: O(1)
def add_first(value)
Comment on lines +19 to +23

Choose a reason for hiding this comment

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

👍

node = Node.new(value)
node.next = @head
@head = 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: ?
def search(value)
raise NotImplementedError
# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
# Time Complexity: O(n)
# Space Complexity: O(1)
def search(value)
Comment on lines +29 to +33

Choose a reason for hiding this comment

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

👍

current = @head
while current != nil
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
def find_max
raise NotImplementedError
# 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(n)
def find_max
Comment on lines +42 to +46

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) since you're not building a new list.

return nil if !@head
current = @head
max = -(Float::INFINITY)
while current != nil
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: ?
def find_min
raise NotImplementedError
# method to return the min value in the linked list
# returns the data value and not the node
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_min
Comment on lines +59 to +63

Choose a reason for hiding this comment

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

👍

return nil if !@head
current = @head
min = Float::INFINITY
while current != nil
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: ?
def length
raise NotImplementedError
# method that returns the length of the singly linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
def length
Comment on lines +76 to +79

Choose a reason for hiding this comment

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

👍

counter = 0
current = @head
while current != nil
counter += 1
current = current.next
end
return counter
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: ?
def get_at_index(index)
raise NotImplementedError
# 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: O(n)
# Space Complexity: O(1)
def get_at_index(index)
Comment on lines +89 to +94

Choose a reason for hiding this comment

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

👍

i = 0
current = @head
while current != nil
return current.data if index == i
i += 1
current = current.next
end
return nil
end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
def visit
raise NotImplementedError
# method to print all the values in the linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
def visit
Comment on lines +105 to +108

Choose a reason for hiding this comment

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

👍

current = @head
while current != nil
puts current.data
current = current.next
end
end

# method to delete the first node found with specified value
# Time Complexity: ?
# Space Complexity: ?
def delete(value)
raise NotImplementedError
# method to delete the first node found with specified value
# Time Complexity: O(1)
# Space Complexity: O(1)
def delete(value)
Comment on lines +116 to +119

Choose a reason for hiding this comment

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

👍 , However since this method traverses the list until it finds the given value and then deletes it. So the time complexity is O(n)

return nil if !@head
if @head.data == value
@head = @head.next
return
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: ?
def reverse
raise NotImplementedError
current = @head
while current != nil
if current.next.data == value
current.next = current.next.next
return
end
current = current.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: O(n)
# Space Complexity: O(1)
def reverse
Comment on lines +135 to +139

Choose a reason for hiding this comment

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

👍 , nicely done.

prev = nil
next_node = nil
current = @head
while current != nil

next_node = current.next
current.next = prev
prev = current
current = next_node
end
@head = prev
end

## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
# Space Complexity: ?
def find_middle_value
raise NotImplementedError
## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_middle_value
Comment on lines +153 to +157

Choose a reason for hiding this comment

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

👍 , Well done. I like the slow and fast pointers. Well done.

return nil if !@head
slow = @head
fast = @head
while fast.next != nil
slow = slow.next
fast = fast.next.next
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: ?
def find_nth_from_end(n)
raise NotImplementedError
# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_nth_from_end(n)
return nil if !@head
current = @head
index = 1
while current != nil
if length - index == n

Choose a reason for hiding this comment

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

Just a note, that length is a method that traverses the list each time. So this makes it O(n^2) time complexity.

Better would be to save the length into a local variable and then use that local variable in the loop.

return current.data
end
index += 1
current = current.next
end
return nil
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: ?
def has_cycle
raise NotImplementedError
# 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: ?
def has_cycle
Comment on lines +186 to +191

Choose a reason for hiding this comment

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

👍 , but what would the time complexity be?

return false if !@head || @head.next = nil
slow = @head
fast = @head
while fast != nil
slow = slow.next
fast = fast.next
if fast != nil
fast = fast.next
end
return true if fast == slow
end
return false
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
Comment on lines +206 to +211

Choose a reason for hiding this comment

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

👍

return @head.data if @head
return nil
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
# method that inserts a given value as a new last node in the linked list
# Time Complexity: O(1)
# Space Complexity: O(1)
def add_last(value)
Comment on lines +216 to +219

Choose a reason for hiding this comment

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

This method works, but since you traverse the list until the end of the list... the time complexity is O(n)

new_node = Node.new(value)
if !@head
@head = new_node
return
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
current = @head
while current.next != nil
current = current.next
end
current.next = new_node
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
# method that returns the value of the last node in the linked list
# returns nil if the linked list is empty
# Time Complexity: O(1)
# Space Complexity: O(1)
def get_last
Comment on lines +232 to +236

Choose a reason for hiding this comment

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

Similar issues to the above method.

return nil if @head == nil
current = @head
while current.next != nil
current = current.next
end
return current.data
end

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

Choose a reason for hiding this comment

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

👍

new_node = Node.new(value)
if @head == nil
@head = new_node
return
end
current = @head
while current.next && current.next.value <= value
current = current.next
end
new_node.next = current.next
current.next = new_node
end

# Helper method for tests
# Creates a cycle in the linked list for testing purposes
# Assumes the linked list has at least one node
def create_cycle
return if @head == nil # don't do anything if the linked list is empty

# navigate to last node
current = @head
while current.next != nil
current = current.next
end
# Helper method for tests
# Creates a cycle in the linked list for testing purposes
# Assumes the linked list has at least one node
def create_cycle
return if @head == nil # don't do anything if the linked list is empty

current.next = @head # make the last node link to first node
# navigate to last node
current = @head
while current.next != nil
current = current.next
end

current.next = @head # make the last node link to first node
end
end