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
6 changes: 6 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .idea/linked-list.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

177 changes: 124 additions & 53 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,148 +12,219 @@ 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
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.

👍 Time/space complexity?

@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: ?
def search(value)
raise NotImplementedError
def search(value)
Comment on lines 29 to +31

Choose a reason for hiding this comment

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

👍 Time/space complexity?

current = @head

until current == nil
if current.data == value
return true
end
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
def find_max
Comment on lines 43 to +45

Choose a reason for hiding this comment

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

👍 Time/space complexity?

current = @head

return nil if current.nil?

max = @head.data

until current == nil
if current.data > max
max = current.data
current = current.next
else
current = current.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: ?
def find_min
raise NotImplementedError
def find_min
Comment on lines 65 to +67

Choose a reason for hiding this comment

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

👍 Time/space complexity?

current = @head

return nil if @head.nil?

min = @head.data

until current == nil
if current.data < min
min = current.data
current = current.next
else
current = current.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: ?
def get_first
raise NotImplementedError
end
def get_first
Comment on lines 89 to +91

Choose a reason for hiding this comment

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

👍 Time/space complexity?

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)
raise NotImplementedError
def add_last(value)
Comment on lines 96 to +98

Choose a reason for hiding this comment

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

👍 Time/space complexity?

new_node = Node.new(value)

if @head.nil?
@head = new_node
end

current = @head

until current.next.nil?
current = current.next
end
current.next = new_node
end

# method that returns the length of the singly linked list
# Time Complexity: ?
# Space Complexity: ?
def length
raise NotImplementedError
def length
Comment on lines 114 to +116

Choose a reason for hiding this comment

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

👍 Time/space complexity?

current = @head

length = 0

until current.nil?
current = current.next
length += 1
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: ?
def get_at_index(index)
raise NotImplementedError
def get_at_index(index)
Comment on lines 131 to +133

Choose a reason for hiding this comment

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

👍 Time/space complexity?

return nil if @head.nil?

current = @head

index.times do
if current.next.nil?
return nil
else
current = current.next
end
end
return current.data
end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
def visit
raise NotImplementedError
def visit
Comment on lines 149 to +151

Choose a reason for hiding this comment

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

👍 Time/space complexity?

current = @head

while current
print 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
end
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: ?
def reverse
raise NotImplementedError
end
def reverse
raise NotImplementedError
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
end
def get_last
raise NotImplementedError
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
end
def find_middle_value
raise NotImplementedError
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
end
def find_nth_from_end(n)
raise NotImplementedError
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
end
def has_cycle
raise NotImplementedError
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
end
def insert_ascending(value)
raise NotImplementedError
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
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
Loading