-
Notifications
You must be signed in to change notification settings - Fork 44
Fire - Roshni #18
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?
Fire - Roshni #18
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,3 @@ | ||||||||||||||||||
|
|
||||||||||||||||||
| # Defines a node in the singly linked list | ||||||||||||||||||
| class Node | ||||||||||||||||||
| attr_reader :data # allow external entities to read value but not write | ||||||||||||||||||
|
|
@@ -18,95 +17,199 @@ 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 | ||||||||||||||||||
| new_node = Node.new(value) | ||||||||||||||||||
| new_node.next = @head | ||||||||||||||||||
| @head = new_node | ||||||||||||||||||
| else | ||||||||||||||||||
| @head = Node.new(value) | ||||||||||||||||||
| end | ||||||||||||||||||
|
Comment on lines
+23
to
+29
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. Let me suggest this as simpler.
Suggested change
|
||||||||||||||||||
| 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
+34
to
36
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 | ||||||||||||||||||
| if current.data == value | ||||||||||||||||||
| return true | ||||||||||||||||||
| else | ||||||||||||||||||
| current = current.next | ||||||||||||||||||
| end | ||||||||||||||||||
| 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
+52
to
54
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 | ||||||||||||||||||
| max = current.data | ||||||||||||||||||
|
|
||||||||||||||||||
| until current.nil? | ||||||||||||||||||
| 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(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 | ||||||||||||||||||
| return nil if @head.nil? | ||||||||||||||||||
| current = @head | ||||||||||||||||||
| min = current.data | ||||||||||||||||||
|
|
||||||||||||||||||
| until current.nil? | ||||||||||||||||||
| 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
+91
to
93
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 @head ? @head.data : nil | ||||||||||||||||||
| 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
+98
to
100
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 | ||||||||||||||||||
| current = @head | ||||||||||||||||||
| until current.next.nil? | ||||||||||||||||||
| current = current.next | ||||||||||||||||||
| end | ||||||||||||||||||
| current.next = Node.new(value) | ||||||||||||||||||
| else | ||||||||||||||||||
| @head = Node.new(value) | ||||||||||||||||||
| end | ||||||||||||||||||
| 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
+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 | ||||||||||||||||||
| length = 0 | ||||||||||||||||||
| current = @head | ||||||||||||||||||
|
|
||||||||||||||||||
| while 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
+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 self.length < index || @head.nil? | ||||||||||||||||||
| i = 0 | ||||||||||||||||||
| current = @head | ||||||||||||||||||
|
|
||||||||||||||||||
| while current | ||||||||||||||||||
| if i == index | ||||||||||||||||||
| return current.data | ||||||||||||||||||
| else | ||||||||||||||||||
| current = current.next | ||||||||||||||||||
| i += 1 | ||||||||||||||||||
| end | ||||||||||||||||||
| 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
+147
to
149
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 | ||||||||||||||||||
| print 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
+160
to
162
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? | ||||||||||||||||||
|
|
||||||||||||||||||
| if @head.data == value | ||||||||||||||||||
| @head = @head.next | ||||||||||||||||||
| return | ||||||||||||||||||
| else | ||||||||||||||||||
| current = @head | ||||||||||||||||||
| while current.next | ||||||||||||||||||
| if current.next.data == value | ||||||||||||||||||
| current.next = current.next.next | ||||||||||||||||||
| return | ||||||||||||||||||
| end | ||||||||||||||||||
| 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
+182
to
184
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 | ||||||||||||||||||
| prev = nil | ||||||||||||||||||
|
|
||||||||||||||||||
| while current | ||||||||||||||||||
| temp = current.next | ||||||||||||||||||
| current.next = prev | ||||||||||||||||||
| prev = current | ||||||||||||||||||
| current = temp | ||||||||||||||||||
| end | ||||||||||||||||||
| @head = prev | ||||||||||||||||||
| return @head | ||||||||||||||||||
| 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(1) | ||||||||||||||||||
| def get_last | ||||||||||||||||||
|
Comment on lines
+202
to
204
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 | ||||||||||||||||||
| until current.next.nil? | ||||||||||||||||||
| current = current.next | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| return current.data | ||||||||||||||||||
| end | ||||||||||||||||||
|
|
||||||||||||||||||
| ## Advanced Exercises | ||||||||||||||||||
|
|
||||||||||||||||||
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.
👍