-
Notifications
You must be signed in to change notification settings - Fork 44
Water - Jessica #38
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?
Water - Jessica #38
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,79 +18,150 @@ 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) | ||||||||||||||||||||||||||||
|
Comment on lines
+30
to
32
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 | ||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||
| until current.nil? | ||||||||||||||||||||||||||||
| if current.data == value | ||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # method to return the max value in the linked list | ||||||||||||||||||||||||||||
| # returns the data value and not the node | ||||||||||||||||||||||||||||
| # Time Complexity: O(n) | ||||||||||||||||||||||||||||
| # Space Complexity: O(1) | ||||||||||||||||||||||||||||
| def find_max | ||||||||||||||||||||||||||||
|
Comment on lines
+46
to
48
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? | ||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||
| max_value = current.data | ||||||||||||||||||||||||||||
| until current.nil? | ||||||||||||||||||||||||||||
| if current.data > max_value | ||||||||||||||||||||||||||||
| max_value = current.data | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||
| 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: ? | ||||||||||||||||||||||||||||
| # Time Complexity: O(n) | ||||||||||||||||||||||||||||
| # Space Complexity: O(1) | ||||||||||||||||||||||||||||
| def find_min | ||||||||||||||||||||||||||||
|
Comment on lines
+63
to
65
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? | ||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||
| min_value = current.data | ||||||||||||||||||||||||||||
| until current.nil? | ||||||||||||||||||||||||||||
| if current.data < min_value | ||||||||||||||||||||||||||||
| min_value = current.data | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||
| 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
+82
to
84
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: ? | ||||||||||||||||||||||||||||
| # Time Complexity: O(n) | ||||||||||||||||||||||||||||
| # Space Complexity: O(1) | ||||||||||||||||||||||||||||
| def add_last(value) | ||||||||||||||||||||||||||||
|
Comment on lines
+90
to
92
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 | ||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||
| until current.nil? | ||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| current = Node.new(value) | ||||||||||||||||||||||||||||
|
Comment on lines
+94
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. This was not linking in the new node to the list.
Suggested change
|
||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||
|
Comment on lines
+101
to
103
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 | ||||||||||||||||||||||||||||
| length = 0 | ||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||
| until current.nil? | ||||||||||||||||||||||||||||
| length += 1 | ||||||||||||||||||||||||||||
| current = current.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(1) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_at_index(index) | ||||||||||||||||||||||||||||
|
Comment on lines
+116
to
119
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? | ||||||||||||||||||||||||||||
| index_counter = 0 | ||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||
| until index_counter == index || current.nil? | ||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||
| index_counter += 1 | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| if index_counter < index | ||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||
| return current.data | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # method to print all the values in the linked list | ||||||||||||||||||||||||||||
| # Time Complexity: ? | ||||||||||||||||||||||||||||
| # Space Complexity: ? | ||||||||||||||||||||||||||||
| # Time Complexity: O(n) | ||||||||||||||||||||||||||||
| # Space Complexity: O(1) | ||||||||||||||||||||||||||||
| def visit | ||||||||||||||||||||||||||||
|
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. 👍 |
||||||||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||||||||
| return nil if @head.nil? | ||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||
| until current.nil? | ||||||||||||||||||||||||||||
| print current.data | ||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # method to delete the first node found with specified value | ||||||||||||||||||||||||||||
| # Time Complexity: ? | ||||||||||||||||||||||||||||
| # Space Complexity: ? | ||||||||||||||||||||||||||||
| # 0 1 2 nil | ||||||||||||||||||||||||||||
| # 2 | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def delete(value) | ||||||||||||||||||||||||||||
|
Comment on lines
+149
to
152
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? | ||||||||||||||||||||||||||||
| if @head.data == value | ||||||||||||||||||||||||||||
| @head = @head.next | ||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||
| until current.next.nil? | ||||||||||||||||||||||||||||
| if current.next.data == value | ||||||||||||||||||||||||||||
| current.next = current.next.next | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # method to reverse the singly linked list | ||||||||||||||||||||||||||||
|
|
@@ -106,7 +177,11 @@ def reverse | |||||||||||||||||||||||||||
| # Time Complexity: ? | ||||||||||||||||||||||||||||
| # Space Complexity: ? | ||||||||||||||||||||||||||||
| def get_last | ||||||||||||||||||||||||||||
|
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 | ||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||
| until current.next.nil? | ||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| return current.data | ||||||||||||||||||||||||||||
| 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.
👍