-
Notifications
You must be signed in to change notification settings - Fork 44
Water - Mackenzie #19
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?
Changes from all commits
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,63 +18,146 @@ 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 | ||
| # raise NotImplementedError | ||
|
|
||
| new_node = Node.new(value) | ||
| 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(n) | ||
| def search(value) | ||
|
Comment on lines
+35
to
37
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. 👍 , however the space complexity is O(1) because you're not creating new nodes here. |
||
| raise NotImplementedError | ||
| # raise NotImplementedError | ||
| current_node = @head | ||
|
|
||
| while current_node | ||
| if current_node.data == value | ||
| return true | ||
| else | ||
| current_node = current_node.next | ||
| end | ||
| end | ||
|
|
||
| return false | ||
|
|
||
| end | ||
|
|
||
| # method to return the max value in the linked list | ||
| # returns the data value and not the node | ||
| def find_max | ||
|
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. 👍 |
||
| raise NotImplementedError | ||
| # raise NotImplementedError | ||
| if @head == nil | ||
| return nil | ||
| end | ||
|
|
||
| max_value = @head.data | ||
| current_node = @head | ||
|
|
||
| while current_node | ||
| if current_node.data > max_value | ||
| max_value = current_node.data | ||
| current_node = current_node.next | ||
| else | ||
| current_node = current_node.next | ||
| end | ||
| end | ||
|
|
||
|
|
||
| return max_value | ||
| 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 | ||
|
Comment on lines
77
to
81
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 | ||
| # raise NotImplementedError | ||
|
|
||
| if @head == nil | ||
| return nil | ||
| end | ||
|
|
||
| min_value = @head.data | ||
| current_node = @head | ||
|
|
||
| while current_node | ||
| if current_node.data < min_value | ||
| min_value = current_node.data | ||
| current_node = current_node.next | ||
| else | ||
| current_node = current_node.next | ||
| end | ||
| end | ||
|
|
||
|
|
||
| return min_value | ||
| 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
+108
to
110
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 | ||
| # raise NotImplementedError | ||
| if @head == nil | ||
| return nil | ||
| else | ||
| return @head.data | ||
| end | ||
|
|
||
|
|
||
| end | ||
|
|
||
| # method that inserts a given value as a new last node in the linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def add_last(value) | ||
|
Comment on lines
+122
to
124
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. 👍 , however the space complexity is O(1) because you're not creating new nodes. |
||
| raise NotImplementedError | ||
| # raise NotImplementedError | ||
| new_node = Node.new(value) | ||
|
|
||
| if @head == nil | ||
| @head = new_node | ||
| else | ||
| current_node = @head | ||
| until current_node.next == nil do | ||
| current_node = current_node.next | ||
| end | ||
| current_node.next = new_node | ||
| end | ||
| end | ||
|
|
||
| # method that returns the length of the singly linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def length | ||
|
Comment on lines
+140
to
142
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 | ||
| # raise NotImplementedError | ||
| length = 0 | ||
| current_node = @head | ||
|
|
||
| while current_node | ||
| length += 1 | ||
| current_node = current_node.next | ||
| end | ||
|
|
||
| return length | ||
|
|
||
| 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(n) | ||
| def get_at_index(index) | ||
|
Comment on lines
+159
to
161
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. Just noting this isn't done and the ones below. |
||
| raise NotImplementedError | ||
| end | ||
|
|
@@ -87,16 +170,16 @@ def visit | |
| end | ||
|
|
||
| # method to delete the first node found with specified value | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| 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(n) | ||
| def reverse | ||
| raise NotImplementedError | ||
| end | ||
|
|
||
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.
👍