-
Notifications
You must be signed in to change notification settings - Fork 48
Time - Alex #26
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 - Alex #26
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,10 +4,11 @@ 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def initialize(value, next_node = nil) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def initialize(value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @data = value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @next = next_node | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Defines the singly linked list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -18,71 +19,161 @@ 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @head = Node.new(value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new_nodelet = Node.new(value, @head) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new_nodelet.next = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @head = new_nodelet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+36
to
38
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 false if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true if value == current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+53
to
55
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. Correct, but space complexity would be O(1) since you don't create a new data structure, but just track the greatest value found so far. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| highest_value = @head.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| until current.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current.data > highest_value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| highest_value = current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return highest_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(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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. Ditto with |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lowest_value = @head.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| until current.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if current.data < lowest_value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lowest_value = current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return lowest_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
+93
to
95
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 0 if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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(1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_at_index(index) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| until count == index | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return current.data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+115
to
+125
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 less than |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
+129
to
131
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? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Space Complexity: O(n) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def delete(value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. What if you are deleting the 1st node? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| last = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| until current.data == value || current.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| last = current | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| last.next = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+147
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.
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
+161
to
163
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. 👍 Nicely done |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| last = nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while current != nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| queued = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current.next = last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| last = current | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| current = queued | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @head = last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "folders": [ | ||
| { | ||
| "path": "../.." | ||
| }, | ||
| { | ||
| "path": ".." | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| @next = next_node |
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.
You could rewrite this as:
This way the new node's next points to the old head (which might be
nil).