-
Notifications
You must be signed in to change notification settings - Fork 48
Space - Hala #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?
Space - Hala #37
Changes from all commits
4284925
39d1fd0
c4d87ad
62cb45b
27b7605
2602c49
be09127
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,13 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 :prev # allow external entities to read or write next node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def initialize(value, next_node = nil) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def initialize(value, next_node = nil, prev_node = nil) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @data = value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @next = next_node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @prev = prev_node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -18,71 +21,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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @head = Node.new(value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| first = Node.new(value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| first.next = current | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current.prev = first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @head = first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+40
to
42
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current.next != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current.data == value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current.data == value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+65
to
67
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max = -1000000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elsif @head.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return @head.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current.next != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current.data > max | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max = current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current.data > max | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max = current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+92
to
94
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| min = 1000000 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elsif @head.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return @head.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current.next != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current.data < min | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| min = current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current.data < min | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| min = current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+119
to
121
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 = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elsif current.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current.next != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| length +=1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+122
to
+134
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 could be simplified a bit:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+140
to
142
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if index > self.length-1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if index == 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return @head.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current.next != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if count == index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if index == self.length-1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+166
to
168
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current.next != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| puts current.value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| puts current.value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if @head.next.nil? && @head.data == value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @head = nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elsif @head.next != nil && @head.data == value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @head = @head.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elsif current.next != nil && current.prev != nil && current.data == value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prevn = current.prev | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nextn = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prevn.next = nextn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nextn.prev = prevn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elsif !current.prev.nil? && current.data == value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current.prev.next = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+184
to
+199
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 is a bit overcomplicated
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+204
to
206
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.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current.next != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @head = current | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| prev = current.prev | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current.prev = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current.next = prev | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = prev | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # if temp != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # @head = temp.prev | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -225,4 +225,4 @@ | |
| expect(@list.get_at_index(3)).must_equal 1 | ||
| end | ||
| end | ||
| end | ||
| end | ||
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.
👍