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

Kate N #26

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
52 changes: 34 additions & 18 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,44 @@ 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)
# o(1) b/c pointers at head + tail
def add_first(value)
raise NotImplementedError
node = Node.new data

Choose a reason for hiding this comment

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

You're not changing @head at all.

self.length +=1
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(n)
def search(value)
raise NotImplementedError
return value unless
end

# method to return the max value in the linked list
# returns the data value and not the node
# Time Complexity:
# Space Complexity
def find_max
raise NotImplementedError

end

# method to return the min value in the linked list
# returns the data value and not the node
# Time Complexity:
# Space Complexity
def find_min
raise NotImplementedError

end


# method that returns the length of the singly linked list
# Time Complexity:
# Space Complexity
def length
raise NotImplementedError

end

# method that returns the value at a given index in the linked list
Expand All @@ -62,29 +64,43 @@ def length
# Time Complexity:
# Space Complexity
def get_at_index(index)
raise NotImplementedError
current_node = @head
while current_node.next?
current_node = current_node.next
end

# method to print all the values in the linked list
# Time Complexity:
# Space Complexity
def visit
raise NotImplementedError

end

# method to delete the first node found with specified value
# Time Complexity:
# Space Complexity
#### method to delete the first node found with specified value
# Time Complexity: o(n)
# Space Complexity: o(n)
def delete(value)
raise NotImplementedError
if self.value = true
remove self
end

# method to reverse the singly linked list
#### 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
# Space Complexity:
# do with + w/o recursion ?
def reverse

Choose a reason for hiding this comment

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

👍

Since you didn't make any external data structures, your space complexity here is O(1), and since you have to traverse the list once, your time complexity is O(n). You can usually guess this by the fact that there is one and only one loop.

raise NotImplementedError
current_node = @head
previous_node = nil
next_node = nil

while current_node
next_node = current_node.pointer
current_node.set_pointer(previous_node)
previous_node = current_node
current_node = next_node
end
@head = previous_node
end


Expand Down