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

Nara #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Nara #27

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
132 changes: 100 additions & 32 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Defines a node in the singly linked list
class Node
attr_reader :data # allow external entities to read value but not write
Expand All @@ -18,71 +17,122 @@ 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
new_node = Node.new(value, @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)
raise NotImplementedError
current = @head
while current
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:
# Space Complexity
# Time Complexity: o(n)
# Space Complexity: O(1)
def find_max
raise NotImplementedError
if @head == nil
return
end
current = @head
max = current.data
while current
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
raise NotImplementedError
if @head == nil
return
end
current = @head
min = current.data
while current
if current.data < min
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
raise NotImplementedError
current = @head
count = 0
while current
count += 1
current = current.next
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
current = @head
count = 0
while current
if count == index
return current.data
elsif count > index
return
end
count += 1
current = current.next
end
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
raise NotImplementedError
current = @head
linked_list_values = []
while current
linked_list_values.push(current.data)
current = current.next
end
return linked_list_values
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)
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
# Time Complexity: O(n)
# Space Complexity: O(1)
def reverse
raise NotImplementedError
end
Expand Down Expand Up @@ -117,25 +167,43 @@ def has_cycle
# 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
raise NotImplementedError
if @head == nil
return
else
return @head.data
end
end

# method that inserts a given value as a new last node in the linked list
# Time Complexity:

Choose a reason for hiding this comment

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

space & time?

# Space Complexity
def add_last(value)
raise NotImplementedError
new_node = Node.new(value)
if @head == nil
@head = new_node
return
else
current = @head
while current.next != nil
current = current.next
end
current.next = new_node
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:

Choose a reason for hiding this comment

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

Space and time complexity?

# Space Complexity
def get_last
raise NotImplementedError
current = @head
while current
current = current.next
end
return current.data

Choose a reason for hiding this comment

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

What if @head is nil?

end

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