Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Space - Nora #27

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
262 changes: 221 additions & 41 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,232 @@ 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)
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
@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: O(n)
# Space Complexity: O(1)
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.

👍

raise NotImplementedError
if @head.nil?
return false
end

current = @head

while 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
# Time Complexity: O(n)
# Space Complexity: O(1)
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
if @head.nil?
return nil
end

max = @head.data
current = @head
while current != nil
if max < current.data
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 +72 to 74

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?
return nil
end

min = @head.data
current = @head
while current != nil
if min > current.data
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: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def length
Comment on lines +94 to 96

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?
return 0
end

length = 0
current = @head
while current != nil
length += 1
current = current.next
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: ?
# Time Complexity: o(n)
# Space Complexity: o(1)
def get_at_index(index)
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.

👍

raise NotImplementedError
if @head.nil?
return nil
end

count = 0
current = @head
while current != nil
if count == index
return current.data
end
count += 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(n)
def visit
Comment on lines +135 to 137

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) because you're not creating a new list here.

raise NotImplementedError
if @head.nil?
return
end

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

return
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 +152 to 154

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?
return
end

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

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

previous = current
current = current.next
end

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: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def reverse
Comment on lines +182 to 184

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?
return
end

current = @head
previous = nil

while current != nil
temp = current.next
current.next = previous
previous = current
current = temp
end

@head = previous
return
end


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

Choose a reason for hiding this comment

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

👍 , nice use of the fast and slow pointers.

raise NotImplementedError
if @head.nil?
return
end

slow = @head
fast = @head.next
while fast != nil
slow = slow.next
fast = fast.next
if fast != nil
fast = fast.next
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)
# Space Complexity: O(1)
def find_nth_from_end(n)
Comment on lines +228 to 230

Choose a reason for hiding this comment

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

👍 Well done

raise NotImplementedError
if @head.nil?
return
end

back = @head
front = @head
count = 1
while front != nil
front = front.next
count += 1
if front > n
back = back.next
end
end

return back.data
end

# checks if the linked list has a cycle. A cycle exists if any node in the
Expand All @@ -108,32 +252,68 @@ def find_nth_from_end(n)
# Time Complexity: ?
# Space Complexity: ?
def has_cycle
Comment on lines 252 to 254

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
if @head.nil?
return
end

slow = @head
fast = @head.next
while fast != nil
slow = slow.next
fast = fast.next
if fast != nil
fast = fast.next
end
if fast == slow
return true
end
end

return false
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)
# Space Complexity: O(1)
def get_first
Comment on lines +279 to 281

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return @head.data if @head != nil
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def add_last(value)
Comment on lines +286 to 288

Choose a reason for hiding this comment

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

👍

raise NotImplementedError

current = @head
newNode = Node.new(value, nil)

if current.nil?
@head = newNode
else
until current.next.nil?
current = current.next
end
current.next = newNode
end
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(n)
def get_last
Comment on lines +305 to 307

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?
return nil
else
current = @head
until current.next.nil?
current = current.next
end
end
return current.data
end

# method to insert a new node with specific data value, assuming the linked
Expand Down