-
Notifications
You must be signed in to change notification settings - Fork 48
Time - Emily #45
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?
Time - Emily #45
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,88 +18,213 @@ 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 | ||
| if !@head | ||
| return false | ||
| end | ||
|
|
||
| current = @head | ||
|
|
||
| until current == nil || current.data == value | ||
| current = current.next | ||
| end | ||
|
|
||
| if current | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| # method to return the max value in the linked list | ||
| # returns the data value and not the node | ||
| def find_max | ||
|
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 nil | ||
| end | ||
|
|
||
| max = @head | ||
| current = @head | ||
|
|
||
| until !current | ||
| if current.data > max.data | ||
| max = current | ||
| end | ||
|
|
||
| current = current.next | ||
| end | ||
|
|
||
| return max.data | ||
| 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
+73
to
75
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 nil | ||
| end | ||
|
|
||
| min = @head | ||
| current = @head | ||
|
|
||
| until !current | ||
| if min.data > current.data | ||
| min = current | ||
| end | ||
|
|
||
| current = current.next | ||
| end | ||
|
|
||
| return min.data | ||
| end | ||
|
|
||
|
|
||
| # method that returns the length of the singly linked list | ||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(1) | ||
| # Space Complexity: O(1) | ||
| def length | ||
|
Comment on lines
+96
to
98
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 | ||
| 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
+113
to
115
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 nil | ||
| end | ||
|
|
||
| current_index = 0 | ||
| current = @head | ||
|
|
||
| until !current || current_index == index | ||
| current_index += 1 | ||
| current = current.next | ||
| end | ||
|
|
||
| if current_index == index | ||
| return current.data | ||
| else | ||
| return nil | ||
| 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
+136
to
138
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 | ||
| p 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
+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 | ||
| if !@head | ||
| return nil | ||
| elsif @head.data == value | ||
| @head = @head.next | ||
| end | ||
|
|
||
| previous = nil | ||
| current = @head | ||
|
|
||
| until !current | ||
| if current.data == value && previous | ||
| previous.next = current.next | ||
| end | ||
|
|
||
| if current != nil | ||
| previous = current | ||
| current = current.next | ||
| end | ||
| 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
+174
to
176
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_node = @head | ||
| previous_node = nil | ||
| next_node = nil | ||
|
|
||
| while current_node | ||
| next_node = current_node.next | ||
| current_node.next = previous_node | ||
| previous_node = current_node | ||
| current_node = next_node | ||
| end | ||
|
|
||
| @head = previous_node | ||
|
|
||
| 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(1) | ||
| # unsure if this method actually works - did a brute force/pseudocode answer and didn't check because no tests | ||
| def find_middle_value | ||
|
Comment on lines
+195
to
198
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 nil | ||
| end | ||
|
|
||
| middle = length() / 2 | ||
|
|
||
| return get_at_index(middle).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(1) | ||
| def find_nth_from_end(n) | ||
|
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. 👍 |
||
| raise NotImplementedError | ||
| index_from_end = (length() - n) | ||
|
|
||
| index = 1 | ||
| current = @head | ||
|
|
||
| until !current || index == index_from_end | ||
| current = current.next | ||
| index += 1 | ||
| end | ||
|
|
||
| if !current | ||
| return nil | ||
| else | ||
| return current.data | ||
| end | ||
| end | ||
|
|
||
| # checks if the linked list has a cycle. A cycle exists if any node in the | ||
|
|
@@ -115,25 +240,49 @@ 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
+243
to
245
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 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(1) | ||
| def add_last(value) | ||
|
Comment on lines
+254
to
256
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 | ||
| @head = Node.new(value) | ||
| end | ||
|
|
||
| current = @head | ||
|
|
||
| until current.next == nil | ||
| current = current.next | ||
| end | ||
|
|
||
| current.next = Node.new(value) | ||
| 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: ? | ||
| 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 | ||
| if !@head | ||
| return nil | ||
| end | ||
|
|
||
| current = @head | ||
|
|
||
| until !current.next | ||
| current = current.next | ||
| end | ||
|
|
||
| return current.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.
👍