-
Notifications
You must be signed in to change notification settings - Fork 44
Earth | Ana #31
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 | Ana #31
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,56 +18,111 @@ 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: 0(1) | ||
| # Space Complexity: 0(1) | ||
| def add_first(value) | ||
| raise NotImplementedError | ||
| # 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: ? | ||
| # Time Complexity: 0(n) | ||
| # Space Complexity: 0(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 | ||
| # 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(n) | ||
| def find_max | ||
|
Comment on lines
+47
to
49
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 works, but the space complexity is O(1). |
||
| raise NotImplementedError | ||
| # raise NotImplementedError | ||
| return nil if @head.nil? | ||
| # whatever value in head is now max value | ||
| max = @head.data | ||
| current = @head | ||
| #until current(full node)reaches #end of list | ||
| until current == nil | ||
| #we will go into this conditional | ||
| if current.data > max | ||
| max = current.data | ||
| end | ||
| current = current.next | ||
| end | ||
| return max | ||
| 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
+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 | ||
| end | ||
| # raise NotImplementedError | ||
| return nil if @head.nil? | ||
|
|
||
| min = @head.data | ||
| current = @head | ||
|
|
||
| until current == nil | ||
| if current.data < min | ||
| min = current.data | ||
| end | ||
| current = current.next | ||
| end | ||
| return min | ||
| end | ||
|
|
||
| # Additional Exercises | ||
| # returns the value in the first node | ||
| # returns nil if the list is empty | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: 0(n) | ||
| # Space Complexity: 0(1) | ||
| def get_first | ||
|
Comment on lines
+89
to
91
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 works, but both the time and space complexity are O(1). |
||
| raise NotImplementedError | ||
| # raise NotImplementedError | ||
| return @head.nil? ? nil : @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) | ||
|
Comment on lines
97
to
99
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. 👍 , time/space complexity? |
||
| raise NotImplementedError | ||
| # raise NotImplementedError | ||
| if @head == nil | ||
| @head = Node.new(value) | ||
| else | ||
| current =@head | ||
| #until we go through the full linked list | ||
| until current.next == nil | ||
| current = current.next | ||
| end | ||
| #when next = nil we have reached #end of list so now we add new node | ||
| current.next = Node.new(value) | ||
| end | ||
| 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
+115
to
117
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 | ||
| count = 0 | ||
| current = @head | ||
| until current == nil | ||
| current = current.next | ||
| count += 1 | ||
| end | ||
| return count | ||
| end | ||
|
|
||
| # method that returns the value at a given index in the linked list | ||
|
|
@@ -76,38 +131,90 @@ def length | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def get_at_index(index) | ||
|
Comment on lines
131
to
133
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. 👍 , but space/time complexity? |
||
| raise NotImplementedError | ||
| # raise NotImplementedError | ||
| return nil if @head.nil? | ||
|
|
||
| counter = 0 | ||
| current = @head | ||
| until current.nil? | ||
| return current.data if counter == index | ||
| counter += 1 | ||
| current = current.next | ||
| end | ||
| return nil | ||
| 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
+148
to
150
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 | ||
| current = @head | ||
| until current.nil? | ||
| puts current.data | ||
| current = current.next | ||
| end | ||
| end | ||
|
|
||
| # method to delete the first node found with specified value | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: o(n) | ||
| # Space Complexity: o(1) | ||
| def delete(value) | ||
|
Comment on lines
+160
to
162
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 | ||
|
|
||
| if @head.data == value | ||
| @head = @head.next | ||
| end | ||
|
|
||
| current = @head | ||
| until current.next == nil | ||
| if current.next.data == value | ||
| current.next = current.next.next | ||
| end | ||
| current = current.next | ||
| end | ||
| 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(1) | ||
| def reverse | ||
|
Comment on lines
+183
to
185
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 | ||
|
|
||
| current =@head | ||
| previous =nil | ||
|
|
||
| until current.nil? | ||
| temp = current.next | ||
| current.next = previous | ||
| previous = current | ||
| current = temp | ||
| end | ||
|
|
||
| @head = previous | ||
|
|
||
|
|
||
| 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: ? | ||
| # Time Complexity: o(n) | ||
| # Space Complexity: o(1) | ||
| def get_last | ||
|
Comment on lines
+205
to
207
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 | ||
| return nil if @head.nil? | ||
| current = @head | ||
| until current.next.nil? | ||
| current =current.next | ||
| end | ||
| return current.data | ||
| end | ||
|
|
||
| # @tail.nil? ? nil: @tail.data | ||
|
|
||
| ## Advanced Exercises | ||
| # returns the value at the middle element in the singly linked list | ||
|
|
||
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.
👍