-
Notifications
You must be signed in to change notification settings - Fork 48
Vera - Time #32
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?
Vera - Time #32
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 |
|---|---|---|
|
|
@@ -21,46 +21,110 @@ def initialize | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def add_first(value) | ||
| raise NotImplementedError | ||
| new_node = Node.new(value) | ||
| if @head.nil? | ||
| @head = new_node | ||
| return @head | ||
| end | ||
|
|
||
| new_node.next = @head | ||
| # conection between the new node and the next one that is the head. | ||
| @head = new_node # The new node becomes the 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: O(n) | ||
| # Space Complexity: O(n) | ||
| def search(value) | ||
| raise NotImplementedError | ||
| return false if @head.nil? | ||
|
|
||
| pointer = @head | ||
| return true if pointer.data == value | ||
|
|
||
| while !pointer.next.nil? | ||
| pointer = pointer.next | ||
| return true if pointer.data == value | ||
| end | ||
|
|
||
| # while (pointer = pointer.next) | ||
| # return true if pointer.data == value | ||
| # 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
56
to
58
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? | ||
|
|
||
| pointer = @head | ||
|
|
||
| max = pointer.data | ||
|
|
||
| while !pointer.next.nil? | ||
| pointer = pointer.next | ||
| if pointer.data > max | ||
| max = pointer.data | ||
| end | ||
| 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(n) | ||
| def find_min | ||
|
Comment on lines
+77
to
79
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. 👍 , except the space complexity is O(1) |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
|
|
||
| pointer = @head | ||
| min = pointer.data | ||
|
|
||
| while !pointer.next.nil? | ||
| pointer = pointer.next | ||
| if pointer.data < min | ||
| min = pointer.data | ||
| end | ||
| end | ||
|
|
||
| return min | ||
| 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
+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. 👍 , except space complexity is O(1) |
||
| raise NotImplementedError | ||
| return 0 if @head.nil? | ||
|
|
||
| count = 0 | ||
| current = @head | ||
| while !current.nil? | ||
| count += 1 | ||
| current = current.next | ||
| end | ||
| return count | ||
|
|
||
| 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
+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. What if the length is <= index? You've got to do something to handle that edge case. |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
|
|
||
| pointer = @head | ||
| count = 0 | ||
| until pointer.next.nil? || count == index | ||
| pointer = pointer.next | ||
| count += 1 | ||
| end | ||
|
|
||
| return pointer.data | ||
| end | ||
|
|
||
| # method to print all the values in the linked list | ||
|
|
@@ -71,12 +135,32 @@ 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 | ||
|
Comment on lines
+138
to
-77
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 of course space complexity. |
||
| return nil if @head.nil? | ||
|
|
||
| if @head.data == value | ||
| @head = @head.next | ||
| return | ||
| end | ||
|
|
||
| pointer = find_previous(value) | ||
|
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. Nice use of a helper. |
||
| pointer.next = pointer.next.next | ||
| end | ||
|
|
||
|
|
||
| def find_previous(value) | ||
| current = @head | ||
| return nil if !current.next | ||
|
|
||
| return current if current.next.data == value | ||
|
|
||
| while (current = current.next) | ||
| return current if current.next && current.next.data == value | ||
| 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: ? | ||
|
|
@@ -115,25 +199,47 @@ def has_cycle | |
| # 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
+202
to
204
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(1) | ||
| # Space Complexity: O(1) | ||
| def add_last(value) | ||
|
Comment on lines
+210
to
212
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 time complexity is O(n) because you are traveling to the rear of the list. |
||
| raise NotImplementedError | ||
| new_node = Node.new(value) | ||
| # Using add_first function to add new node in case the list is empty. | ||
| if @head.nil? | ||
| add_first(new_node.data) | ||
| return | ||
| end | ||
|
|
||
| pointer = @head | ||
| # Until we reach the last node | ||
| until pointer.next.nil? | ||
| # Move the pointer to the next. | ||
| pointer = pointer.next | ||
| end | ||
|
|
||
| pointer.next = new_node | ||
| new_node.next = nil | ||
| 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(n) | ||
| def get_last | ||
|
Comment on lines
+233
to
235
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 the space complexity is O(1) |
||
| raise NotImplementedError | ||
| return nil if @head.nil? | ||
| pointer = @head | ||
|
|
||
| until pointer.next.nil? | ||
| pointer = pointer.next | ||
| end | ||
| return pointer.data | ||
| end | ||
|
|
||
| # method to insert a new node with specific data value, assuming the linked | ||
|
|
||
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.
The space complexity is O(1) because you're not creating a new list as this method runs.