-
Notifications
You must be signed in to change notification settings - Fork 44
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
Fire - Anna #17
base: master
Are you sure you want to change the base?
Fire - Anna #17
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done Anna, you hit the learning goals here. Nice work!
# method to find if the linked list contains a node with specified value | ||
# returns true if found, false otherwise | ||
# Time Complexity: O(n) | ||
# >> Must check every single node in linked list | ||
# Space Complexity: O(1) | ||
# >> Just checking for membership; only ever store current_node | ||
def search(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# 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: O(1) | ||
# >> Constant num of actions - create new node, | ||
# set next to old head, set head pointer to new node | ||
# Space Complexity: O(1) | ||
# >> Always adding 1 new node | ||
def add_first(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# method to return the max value in the linked list | ||
# returns the data value and not the node | ||
def find_max |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# Time Complexity: O(n) | ||
# >> have to traverse entire linked list (unless we know it's sorted...) | ||
# Space Complexity: O(1) | ||
# >> store current_node and min regardless of how long LL is | ||
def find_min |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# Time Complexity: O(n) | ||
# >> get length of LL (pass through all n nodes) | ||
# >> go through half of those nodes | ||
# Space Complexity: O(1) | ||
# >> store mid_index, current_node, current_node_num | ||
def find_middle_value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
mid_index = length / 2 | ||
current_node = @head | ||
current_node_num = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but could you do something similar to the method to find a cycle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see - I think India mentioned in the previous CS Fun session that you could move a slow and fast pointer and when fast reaches the end, slow will be at the middle
# Time Complexity: O(n) | ||
# >> pass through all nodes to get length | ||
# >> at worst pass through all nodes again to get 0th node from end | ||
# >> 2n --> n | ||
# Space Complexity: O(1) | ||
# >> store len, index, current_node, curr_node_num | ||
def find_nth_from_end(n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Good use of length.
# Time Complexity: O(n) | ||
# >> If no cycle, 'fast' and 'slow' pointers will go through LL once | ||
# >> If cycle, 'slow' pointer will go through the list at most once | ||
# >> b/c 'fast' cannot lap / overtake the 'slow' pointer if 'fast' | ||
# >> moves two nodes at a time while 'slow' 1 node at a time | ||
# >> if DID skip over --> slow --> fast | ||
# >> previous step would mean both pointers were at the same node | ||
# Space Complexity: O(1) | ||
# >> fast and slow nodes | ||
def has_cycle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
# Time Complexity: O(n) | ||
# >> worst case node has to be placed at end | ||
# Space Complexity: O(1) | ||
# >> store new_node and current_node | ||
def insert_ascending(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Co-authored-by: Chris M <[email protected]>
No description provided.