-
Notifications
You must be signed in to change notification settings - Fork 44
Earth/Taylor #37
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/Taylor #37
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,128 +18,263 @@ 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(n) | ||
| 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. 👍 , however the space complexity is O(1) |
||
| raise NotImplementedError | ||
| return false if @head == nil | ||
| current = @head | ||
| while current | ||
| if current.data == value | ||
| return true | ||
| end | ||
| current = current.next | ||
| 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
+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. 👍 , however the space complexity is O(1) |
||
| raise NotImplementedError | ||
| return nil if @head == nil | ||
| current = @head | ||
| max = current.data | ||
| current = current.next | ||
| while current | ||
| 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(n) | ||
| def find_min | ||
|
Comment on lines
+64
to
66
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) |
||
| raise NotImplementedError | ||
| return nil if @head == nil | ||
| current = @head | ||
| min = current.data | ||
| current = current.next | ||
| while current | ||
| 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: O(1) | ||
| # Space Complexity: O(1) | ||
| def get_first | ||
|
Comment on lines
+84
to
86
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 | ||
| if @head | ||
| return @head.data | ||
| else | ||
| return nil | ||
| 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
+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. 👍 , however the space complexity is O(1) since you're only making 1 node no matter how long the list is. |
||
| raise NotImplementedError | ||
| new_node = Node.new(value) | ||
| if @head == nil | ||
| @head = new_node | ||
| return | ||
| end | ||
| current = @head | ||
| while current.next | ||
| current = current.next | ||
| end | ||
| current.next = new_node | ||
| 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
+111
to
113
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) |
||
| raise NotImplementedError | ||
| return 0 if @head == nil | ||
| current = @head | ||
| count = 0 | ||
| until 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
+127
to
129
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) |
||
| raise NotImplementedError | ||
| current = @head | ||
| current_index = 0 | ||
| if current.nil? || index.nil? | ||
| return nil | ||
| end | ||
| until current_index == index | ||
| current = current.next | ||
| current_index += 1 | ||
| end | ||
| return current.data | ||
| end | ||
|
|
||
| # method to print all the values in the linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def visit | ||
|
Comment on lines
+143
to
145
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) |
||
| raise NotImplementedError | ||
| current = @head | ||
| while current.next | ||
| 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(1) | ||
| # Space Complexity: O(1) | ||
| def delete(value) | ||
|
Comment on lines
+154
to
156
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) |
||
| raise NotImplementedError | ||
| return nil if @head == nil | ||
|
|
||
| if @head.data == value | ||
| @head = @head.next | ||
| return | ||
| end | ||
|
|
||
| current = @head | ||
| while current.next | ||
| if current.next.data == value | ||
| current.next = current.next.next | ||
| return | ||
| 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(n) | ||
| def reverse | ||
|
Comment on lines
+176
to
178
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) |
||
| raise NotImplementedError | ||
| return nil if @head == nil | ||
| current = @head | ||
| prev = nil | ||
| while current | ||
| temp = current.next | ||
| current.next = prev | ||
| prev = current | ||
| current = temp | ||
| end | ||
| @head = prev | ||
| 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
+193
to
195
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) |
||
| raise NotImplementedError | ||
| return nil if @head == nil | ||
|
|
||
| current = @head | ||
| while current.next != nil | ||
| current = current.next | ||
| end | ||
| return current.data | ||
| end | ||
|
|
||
| ## Advanced Exercises | ||
| # returns the value at the middle element in the singly linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def find_middle_value | ||
|
Comment on lines
+207
to
209
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) |
||
| raise NotImplementedError | ||
| return nil if @head == nil | ||
|
|
||
| slow = @head | ||
| fast = @head.next | ||
| while fast != nil | ||
| slow = slow.next | ||
| fast = fast.next | ||
| if fast != nil | ||
| fast = fast.next | ||
| end | ||
| end | ||
| return slow.data | ||
| end | ||
|
|
||
| # find the nth node from the end and return its value | ||
| # assume indexing starts at 0 while counting to n | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def find_nth_from_end(n) | ||
|
Comment on lines
+226
to
228
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) |
||
| raise NotImplementedError | ||
| return nil if @head == nil || self.length < n | ||
| return self.get_last if n == 0 | ||
| n = self.length - n | ||
| count = 0 | ||
| current = @head | ||
| until count == n - 1 | ||
| if current.next == nil | ||
| return nil | ||
| end | ||
| current = current.next | ||
| count += 1 | ||
| end | ||
| return current.data | ||
| end | ||
|
|
||
| # checks if the linked list has a cycle. A cycle exists if any node in the | ||
| # linked list links to a node already visited. | ||
| # returns true if a cycle is found, false otherwise. | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def has_cycle | ||
|
Comment on lines
+247
to
249
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) |
||
| raise NotImplementedError | ||
| slow = @head | ||
| fast = @head.next | ||
| while fast != nil | ||
| slow = slow.next | ||
| fast = fast.next | ||
| if fast == slow | ||
| return true | ||
| end | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| # method to insert a new node with specific data value, assuming the linked | ||
| # list is sorted in ascending order | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def insert_ascending(value) | ||
|
Comment on lines
+264
to
266
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) |
||
| raise NotImplementedError | ||
| new_node = Node.new(value) | ||
| if @head == nil || @head.data > value | ||
| new_node.next = @head | ||
| @head = new_node | ||
| end | ||
| current = @head | ||
| while current.next && current.next.data < value | ||
| current = current.next | ||
| end | ||
| new_node.next = current.next | ||
| current.next = new_node | ||
| end | ||
|
|
||
| # Helper method for tests | ||
|
|
||
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.
👍