-
Notifications
You must be signed in to change notification settings - Fork 44
Earth - Anya #35
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
base: master
Are you sure you want to change the base?
Earth - Anya #35
Changes from all commits
5cd7b3c
dc32a6d
27dc83c
2e0a967
27ff70d
222e63f
04302c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,95 +18,126 @@ 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 | ||
| @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: ? | ||
| def search(value) | ||
| raise NotImplementedError | ||
| # Time Complexity: O(n) with n being the length of the linked list. | ||
| # Space Complexity: O(n) recursive pushes frames onto call stack for length of list. | ||
| def search(value, current = @head) | ||
|
Comment on lines
+29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return false if current.nil? | ||
| return true if current.data == value | ||
| search(value, current.next) | ||
| end | ||
|
|
||
| # method to return the max value in the linked list | ||
| # returns the data value and not the node | ||
| def find_max | ||
| raise NotImplementedError | ||
| # Time Complexity: O(n) with n being the length of the linked list. | ||
| # Space Complexity: O(n) | ||
| def find_max(max_value = -1.0/0, current = @head) | ||
|
Comment on lines
+39
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 , interesting way to get - infinity |
||
| return nil if @head.nil? | ||
| return max_value if current.nil? | ||
| if current.data > max_value | ||
| find_max(current.data, current.next) | ||
| else | ||
| find_max(max_value, current.next) | ||
| end | ||
| 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 | ||
| # Time Complexity: O(n) with n being the length of the linked list. | ||
| # Space Complexity: O(n) | ||
| def find_min(min_value = 1.0/0, current = @head) | ||
|
Comment on lines
+53
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return nil if @head.nil? | ||
| return min_value if current.nil? | ||
| if current.data < min_value | ||
| find_min(current.data, current.next) | ||
| else | ||
| find_min(min_value, current.next) | ||
| end | ||
| 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
+68
to
70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
| return @head.data | ||
| 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 | ||
| # Time Complexity: O(n) since we examine each node to find way to the end | ||
| # Space Complexity: O(n) | ||
| def add_last(value, current = @head) | ||
|
Comment on lines
+76
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return add_first(value) if current.nil? #sets head if list is nil | ||
| return current = Node.new(value, nil) if current.nil? | ||
| add_last(value, current.next) | ||
| end | ||
|
|
||
| # method that returns the length of the singly linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def length | ||
| raise NotImplementedError | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def length(current = @head, length = 0) | ||
|
Comment on lines
+85
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return length if current.nil? | ||
| current = length(current.next, length += 1) | ||
| 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: ? | ||
| def get_at_index(index) | ||
| raise NotImplementedError | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def get_at_index(index, current = @head) | ||
|
Comment on lines
+95
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return nil if index > length - 1 | ||
| return current.data if index.zero? | ||
| get_at_index(index - 1, current.next) | ||
| end | ||
|
|
||
| # method to print all the values in the linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def visit | ||
| raise NotImplementedError | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def visit(current = @head) | ||
|
Comment on lines
+104
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return nil if current.nil? | ||
| print current.value | ||
| visit(current.next) | ||
| end | ||
|
|
||
| # method to delete the first node found with specified value | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def delete(value) | ||
| raise NotImplementedError | ||
| # Time Complexity: O(n) due to search, delete is O(1) | ||
| # Space Complexity: O(n) | ||
| def delete(value, previous = @head, current = @head) | ||
|
Comment on lines
+113
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return nil if current.nil? # if list is empty | ||
| return @head = @head.next if @head.data == value # if deleting head, 2nd node becomes head, needs special case because there's no previous | ||
| return previous.next = current.next if current.data == value | ||
| delete(value, previous = current, current = current.next) | ||
| 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: ? | ||
| def reverse | ||
| raise NotImplementedError | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def reverse(current = @head) | ||
|
Comment on lines
+124
to
+126
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return @head = current if current.next.nil? # once hits last node, sets it to head | ||
| reverse(current.next) # runs until reaching last node | ||
| current.next.next = current # second to last node points next of head to itself (and so on) | ||
| current.next = nil # second to last node points next to nil (this gets updated to other values until we reach the first node - which is the whole point) | ||
| 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 | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def get_last(current = @head) | ||
|
Comment on lines
+135
to
+137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| return nil if current.nil? | ||
| return current.data if current.next.nil? | ||
| get_last(current.next) | ||
| end | ||
|
|
||
| ## Advanced Exercises | ||
|
|
||
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.
👍