-
Notifications
You must be signed in to change notification settings - Fork 48
Alicia - Time #28
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?
Alicia - Time #28
Changes from all commits
1ee8d73
cacf5a7
ab9e27c
270e41a
0a1b748
1a92221
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,10 +3,12 @@ | |||||||||||||||||||||
| class Node | ||||||||||||||||||||||
| attr_reader :data # allow external entities to read value but not write | ||||||||||||||||||||||
| attr_accessor :next # allow external entities to read or write next node | ||||||||||||||||||||||
| attr_accessor :previous | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def initialize(value, next_node = nil) | ||||||||||||||||||||||
| def initialize(value, next_node = nil, prev_node = nil) | ||||||||||||||||||||||
| @data = value | ||||||||||||||||||||||
| @next = next_node | ||||||||||||||||||||||
| @previous = prev_node | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -18,71 +20,185 @@ 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 | ||||||||||||||||||||||
| if @head.nil? | ||||||||||||||||||||||
| @head = @tail = Node.new(value) | ||||||||||||||||||||||
| else | ||||||||||||||||||||||
| new_node = Node.new(value) | ||||||||||||||||||||||
| new_node.next = @head | ||||||||||||||||||||||
| @head.previous = new_node | ||||||||||||||||||||||
| @head = new_node | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
| 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
+38
to
40
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 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| until current_node.nil? | ||||||||||||||||||||||
| return true if current_node.data == value | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current_node = current_node.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(1) | ||||||||||||||||||||||
| def find_max | ||||||||||||||||||||||
|
Comment on lines
+54
to
56
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 | ||||||||||||||||||||||
| max_value = current_node.data unless current_node.nil? | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| until current_node.nil? | ||||||||||||||||||||||
| max_value = current_node.data if current_node.data > max_value | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current_node = current_node.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
+71
to
73
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 | ||||||||||||||||||||||
| min_value = current_node.data unless current_node.nil? | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| until current_node.nil? | ||||||||||||||||||||||
| min_value = current_node.data if current_node.data < min_value | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return min_value | ||||||||||||||||||||||
| 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
+88
to
90
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 | ||||||||||||||||||||||
| length = 0 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| until current_node.nil? | ||||||||||||||||||||||
| length += 1 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current_node = current_node.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
+106
to
108
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. 👍 Good use of your length method |
||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||
| length = self.length | ||||||||||||||||||||||
| return nil if length <= index | ||||||||||||||||||||||
| return @head.data if index == 0 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current_index = 0 | ||||||||||||||||||||||
| current_node = @head | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| until current_index == index | ||||||||||||||||||||||
| current_index += 1 | ||||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return current_node.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
+125
to
127
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 it's O(1) space complexity |
||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||
| return nil if @head.nil? | ||||||||||||||||||||||
| current_node = @head | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| until current_node.nil? | ||||||||||||||||||||||
| puts current_node.data | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return | ||||||||||||||||||||||
| 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) | ||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||
| return if @head.nil? | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if @head.data == value | ||||||||||||||||||||||
| @head = nil if @head.next.nil? | ||||||||||||||||||||||
| @head = @head.next unless @head.nil? | ||||||||||||||||||||||
| return | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
Comment on lines
+146
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. This can be simpler, but since you have a doubly linked list you also need to set the previous.
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current_node = @head | ||||||||||||||||||||||
| previous_node = @head.previous | ||||||||||||||||||||||
|
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. Since
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| until current_node.nil? | ||||||||||||||||||||||
| if current_node.next.nil? | ||||||||||||||||||||||
|
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. Here you also need to verify that |
||||||||||||||||||||||
| @tail = previous_node | ||||||||||||||||||||||
| previous_node.next = nil | ||||||||||||||||||||||
| return | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| next_node = current_node.next | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if current_node.data == value | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| next_node.previous = previous_node | ||||||||||||||||||||||
| previous_node.next = next_node | ||||||||||||||||||||||
| @tail = next_node if next_node.next.nil? | ||||||||||||||||||||||
| return | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| previous_node = current_node | ||||||||||||||||||||||
| current_node = current_node.next | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return | ||||||||||||||||||||||
| 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() | ||||||||||||||||||||||
| # Space Complexity: O(n) | ||||||||||||||||||||||
| def reverse | ||||||||||||||||||||||
|
Comment on lines
+181
to
183
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. Space complexity is O(1) and time complexity is O(n) |
||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||
| return if @head.nil? | ||||||||||||||||||||||
| return if @head.next.nil? | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| temp = @head | ||||||||||||||||||||||
| @head = temp | ||||||||||||||||||||||
|
Comment on lines
+187
to
+188
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. huh? temp is head and head is temp? |
||||||||||||||||||||||
| @tail = @head | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| until current == nil | ||||||||||||||||||||||
| temp = current.next | ||||||||||||||||||||||
| current.next = current.previous | ||||||||||||||||||||||
| current.previous = temp | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current = current.previous | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
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. You need to adjust the |
||||||||||||||||||||||
| return | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -96,10 +212,23 @@ def find_middle_value | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| # 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
+215
to
217
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. 👍 , good use of length. |
||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||
| length = self.length | ||||||||||||||||||||||
| return nil if length <= n | ||||||||||||||||||||||
| return @head.data if n == 0 | ||||||||||||||||||||||
| return @tail.data if n == length - 1 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| current_index = length - 1 | ||||||||||||||||||||||
| current_node = @tail | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| until current_index == n | ||||||||||||||||||||||
| current_index -= 1 | ||||||||||||||||||||||
| current_node = current_node.previous | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return current_node.data | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # checks if the linked list has a cycle. A cycle exists if any node in the | ||||||||||||||||||||||
|
|
@@ -115,25 +244,32 @@ 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 | ||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||
| return @head.data unless @head.nil? | ||||||||||||||||||||||
| 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
+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.nil? | ||||||||||||||||||||||
| @head = @tail = Node.new(value) | ||||||||||||||||||||||
| else | ||||||||||||||||||||||
| new_node = Node.new(value) | ||||||||||||||||||||||
| @tail.next = new_node | ||||||||||||||||||||||
| new_node.previous = @tail | ||||||||||||||||||||||
| @tail = new_node | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
| 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(1) | ||||||||||||||||||||||
| # Space Complexity: O(1) | ||||||||||||||||||||||
| def get_last | ||||||||||||||||||||||
|
Comment on lines
+269
to
271
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 @tail.data unless @tail.nil? | ||||||||||||||||||||||
| 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.
👍