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
145 changes: 118 additions & 27 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ class Node
attr_reader :data # allow external entities to read value but not write
attr_accessor :next # allow external entities to read or write next node

def initialize(value, next_node = nil)
def initialize(value)
@data = value
@next = next_node
end

end

# Defines the singly linked list
Expand All @@ -18,71 +19,161 @@ 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)
raise NotImplementedError
end
if @head.nil?
@head = Node.new(value)
else
new_nodelet = Node.new(value, @head)
new_nodelet.next = @head
@head = new_nodelet
end
Comment on lines +25 to +31

Choose a reason for hiding this comment

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

You could rewrite this as:

Suggested change
if @head.nil?
@head = Node.new(value)
else
new_nodelet = Node.new(value, @head)
new_nodelet.next = @head
@head = new_nodelet
end
@head = Node.new(value, @head)

This way the new node's next points to the old head (which might be nil).

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 +36 to 38

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return false if @head.nil?

current = @head

while current != nil
return true if value == current.data
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)
# Space Complexity: O(n)
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.

Correct, but space complexity would be O(1) since you don't create a new data structure, but just track the greatest value found so far.

raise NotImplementedError
return nil if @head.nil?

current = @head
highest_value = @head.data

until current.nil?
if current.data > highest_value
highest_value = current.data
end
current = current.next
end

return highest_value
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(n)
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.

Ditto with find_max

raise NotImplementedError
return nil if @head.nil?

current = @head
lowest_value = @head.data

until current.nil?
if current.data < lowest_value
lowest_value = current.data
end
current = current.next
end

return lowest_value
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 +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
return 0 if @head.nil?

current = @head
count = 0

while current != nil
current = current.next
count += 1
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)
raise NotImplementedError
return nil if @head.nil?

current = @head
count = 0

until count == index
current = current.next
count += 1
end

return current.data
Comment on lines +115 to +125

Choose a reason for hiding this comment

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

What if the length is less than index?

end

# method to print all the values in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def visit
Comment on lines +129 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?

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: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def delete(value)
Comment on lines +143 to 145

Choose a reason for hiding this comment

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

What if you are deleting the 1st node?

raise NotImplementedError
return nil if @head.nil?

last = @head
current = @head

until current.data == value || current.nil?
last = current
current = current.next
end

last.next = current.next
Comment on lines +147 to +156

Choose a reason for hiding this comment

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

Suggested change
last = @head
current = @head
until current.data == value || current.nil?
last = current
current = current.next
end
last.next = current.next
if @head.data == value
@head = @head.next
return
end
last = nil
current = @head
until current.data == value || current.nil?
last = current
current = current.next
end
unless current.nil?
last.next = 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: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def reverse
Comment on lines +161 to 163

Choose a reason for hiding this comment

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

👍 Nicely done

raise NotImplementedError
return nil if @head.nil?

last = nil
current = @head

while current != nil
queued = current.next
current.next = last
last = current
current = queued
end

@head = last
end


Expand Down
10 changes: 10 additions & 0 deletions lib/ll.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"folders": [
{
"path": "../.."
},
{
"path": ".."
}
]
}
1 change: 1 addition & 0 deletions lib/tempCodeRunnerFile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@next = next_node