-
Notifications
You must be signed in to change notification settings - Fork 44
Ida - Fire - Linked Lists #33
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?
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,65 +18,124 @@ 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 | ||||||||
| node = Node.new(value, @head) #head is nil | ||||||||
| @head = 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 | ||||||||
| current_node = @head | ||||||||
|
|
||||||||
| while current_node | ||||||||
| if current_node.data == value | ||||||||
| return true | ||||||||
| else | ||||||||
| current_node = current_node.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
+48
to
50
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 | ||||||||
| return if @head.nil? | ||||||||
| max = current_node.data | ||||||||
|
|
||||||||
| while current_node.next | ||||||||
| if current_node.next.data > max | ||||||||
| max = current_node.next.data | ||||||||
| end | ||||||||
| current_node = current_node.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
+67
to
69
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 | ||||||||
| return if @head.nil? | ||||||||
| min = current_node.data | ||||||||
|
|
||||||||
| while current_node.next | ||||||||
| if current_node.next.data < min | ||||||||
| min = current_node.next.data | ||||||||
| end | ||||||||
| current_node = current_node.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
+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. Clever use of & to handle when |
||||||||
| raise NotImplementedError | ||||||||
| @head&.data | ||||||||
| 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
+95
to
97
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 add_first(value) if @head.nil? #if list is empty, adding to the first of the list is the same as adding to the end of the list | ||||||||
|
|
||||||||
| node = @head | ||||||||
| while !node.next.nil? #when next is nil, we are at the end of the list | ||||||||
| next_node = node.next | ||||||||
| node = next_node | ||||||||
| end | ||||||||
|
|
||||||||
| node.next = Node.new(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
+110
to
112
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 | ||||||||
| node = @head | ||||||||
|
|
||||||||
| while !node.nil? | ||||||||
| count += 1 | ||||||||
| next_node = node.next | ||||||||
| node = next_node | ||||||||
| end | ||||||||
| 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) | ||||||||
|
Comment on lines
+127
to
129
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 | ||||||||
| node = @head | ||||||||
| while count < index | ||||||||
| return if node.nil? | ||||||||
| node = node.next | ||||||||
| count += 1 | ||||||||
| end | ||||||||
|
|
||||||||
| node.data | ||||||||
| end | ||||||||
|
|
||||||||
| # method to print all the values in the linked list | ||||||||
|
|
@@ -87,10 +146,22 @@ def visit | |||||||
| 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? | ||||||||
| current_node = @head | ||||||||
|
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. Typo
Suggested change
|
||||||||
|
|
||||||||
| if current.data == value | ||||||||
| @head = current.next | ||||||||
| else | ||||||||
| while (current.next != nil) && (current.next.data != value) | ||||||||
| current = current.next | ||||||||
| end | ||||||||
| unless current.next == nil | ||||||||
| current.next =current.next.next | ||||||||
| end | ||||||||
| end | ||||||||
| end | ||||||||
|
|
||||||||
| # method to reverse the singly linked list | ||||||||
|
|
@@ -106,7 +177,14 @@ def reverse | |||||||
| # Time Complexity: ? | ||||||||
| # Space Complexity: ? | ||||||||
| def get_last | ||||||||
|
Comment on lines
177
to
179
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. Time/space complexity? |
||||||||
| raise NotImplementedError | ||||||||
| return if @head.nil? | ||||||||
|
|
||||||||
| node = @head #node is the current object | ||||||||
| while !node.next.nil? #iterating through objects until node.next is nil | ||||||||
| # next_node = node.next | ||||||||
| node = node.next | ||||||||
| end | ||||||||
| node.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.
👍