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

Linkedlist submission #36

Open
wants to merge 1 commit 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
183 changes: 136 additions & 47 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,97 +18,160 @@ 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
new_node = Node.new(value, nil)
new_node.next = @head
@head = new_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: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def search(value)
Comment on lines +31 to 33

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
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 +48 to 50

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return if @head == nil

current = @head
max = current.data

while current.next != nil
if max < current.next.data
max = current.next.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 +69 to 71

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
end
return if @head == nil

current = @head
min = current.data

# 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
while current.next != nil
if current.next.data < min
min = current.next.data
end
current = current.next
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
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 +89 to 91

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
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)
Comment on lines +104 to 106

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
current = @head
count = 0
while current !=nil
if index == count
return current.data
end
current = current.next
count += 1
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 +120 to 122

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
return if @head == nil
current = @head

while current.next !=nil
current = current.next
print current.data
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 +133 to 135

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
if current.data == value
@head = current.next
return
end

while current.next != 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: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(1)
def reverse
Comment on lines +156 to 158

Choose a reason for hiding this comment

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

👍

raise NotImplementedError
end
return if @head == nil
current = @head
previous = nil

while !current.nil?
next_node = current.next
current.next = previous
previous = current
current = next_node
end

@head = previous

# 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



## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: ?
Expand All @@ -134,6 +197,32 @@ def has_cycle
raise NotImplementedError
end


# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity: ?
# Space Complexity: ?
def get_first
Comment on lines +203 to +206

Choose a reason for hiding this comment

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

👍

return if @head == nil
return @head.value
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
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

# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
# Time Complexity: ?
Expand Down