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.

7 changes: 7 additions & 0 deletions .idea/.rakeTasks

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

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

169 changes: 138 additions & 31 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,111 @@ 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: 0(1)
# Space Complexity: 0(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
# raise NotImplementedError
@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: 0(n)
# Space Complexity: 0(1)
def search(value)
Comment on lines +30 to 32

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
current = @head
until current == nil
if current.data == value
return true
else
current = current.next
end
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 +47 to 49

Choose a reason for hiding this comment

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

This works, but the space complexity is O(1).

raise NotImplementedError
# raise NotImplementedError
return nil if @head.nil?
# whatever value in head is now max value
max = @head.data
current = @head
#until current(full node)reaches #end of list
until current == nil
#we will go into this conditional
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: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_min
Comment on lines +68 to 70

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
end
# raise NotImplementedError
return nil if @head.nil?

min = @head.data
current = @head

until current == nil
if current.data < min
min = current.data
end
current = current.next
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: 0(n)
# Space Complexity: 0(1)
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.

This works, but both the time and space complexity are O(1).

raise NotImplementedError
# raise NotImplementedError
return @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: ?
def add_last(value)
Comment on lines 97 to 99

Choose a reason for hiding this comment

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

👍 , time/space complexity?

raise NotImplementedError
# raise NotImplementedError
if @head == nil
@head = Node.new(value)
else
current =@head
#until we go through the full linked list
until current.next == nil
current = current.next
end
#when next = nil we have reached #end of list so now we add new node
current.next = Node.new(value)
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 +115 to 117

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
count = 0
current = @head
until current == nil
current = current.next
count += 1
end
return count
end

# method that returns the value at a given index in the linked list
Expand All @@ -76,38 +131,90 @@ def length
# Time Complexity: ?
# Space Complexity: ?
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.

👍 , but space/time complexity?

raise NotImplementedError
# raise NotImplementedError
return nil if @head.nil?

counter = 0
current = @head
until current.nil?
return current.data if counter == index
counter += 1
current = current.next
end
return nil
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 +148 to 150

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
current = @head
until 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(1)
def delete(value)
Comment on lines +160 to 162

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError
if @head == nil
return nil
end

if @head.data == value
@head = @head.next
end

current = @head
until current.next == nil
if current.next.data == value
current.next = current.next.next
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: ?
# Space Complexity: ?
# Time Complexity: o(n)
# Space Complexity: o(1)
def reverse
Comment on lines +183 to 185

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
# raise NotImplementedError

current =@head
previous =nil

until current.nil?
temp = current.next
current.next = previous
previous = current
current = temp
end

@head = previous


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)
# Space Complexity: o(1)
def get_last
Comment on lines +205 to 207

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
#raise NotImplementedError
return nil if @head.nil?
current = @head
until current.next.nil?
current =current.next
end
return current.data
end

# @tail.nil? ? nil: @tail.data

## Advanced Exercises
# returns the value at the middle element in the singly linked list
Expand Down
2 changes: 1 addition & 1 deletion test/linked_list_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
end
end

xdescribe "Optional addLast & getLast" do
describe "Optional addLast & getLast" do
it "will add to the front if the list is empty" do
@list.add_last(1)
expect(@list.get_at_index(0)).must_equal 1
Expand Down