-
Notifications
You must be signed in to change notification settings - Fork 46
Branches, C. Gutierrez #21
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 |
---|---|---|
|
@@ -2,44 +2,106 @@ | |
|
||
class LinkedList | ||
attr_reader :head | ||
|
||
def initialize | ||
@head = nil | ||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(1) | ||
# Space complexity - O(1) | ||
def add_first(data) | ||
|
||
temp = Node.new(data) | ||
temp.next = @head | ||
@head = temp | ||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(1) | ||
# Space complexity - O(1) | ||
def get_first | ||
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. 👍 |
||
|
||
if @head == nil | ||
return nil | ||
end | ||
|
||
return @head.data | ||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(n) | ||
# Space complexity - O(1) | ||
def length | ||
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. 👍 |
||
return 0 | ||
if @head == nil | ||
return 0 | ||
end | ||
|
||
length = 1 | ||
current = @head | ||
|
||
while current.next != nil | ||
length += 1 | ||
current = current.next | ||
end | ||
return length | ||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(n) | ||
# Space complexity - O(1) | ||
def add_last(data) | ||
|
||
|
||
temp = Node.new(data) | ||
current = @head | ||
|
||
if @head == nil | ||
@head = temp | ||
return | ||
elsif current.next == nil | ||
current.next = temp | ||
return | ||
else | ||
current = self.last_node | ||
current.next = temp | ||
Comment on lines
+55
to
+60
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 seems a little overly complicated. Can you think of a way to simplify this a bit? |
||
end | ||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(n) | ||
# Space complexity - O(1) | ||
def get_last | ||
|
||
|
||
current = @head | ||
|
||
if @head == 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. 🥇 |
||
return nil | ||
end | ||
current = self.last_node | ||
return current.data | ||
|
||
end | ||
|
||
# Time complexity - ? | ||
# Space complexity - ? | ||
# Time complexity - O(n) | ||
# Space complexity - O(1) | ||
def get_at_index(index) | ||
|
||
|
||
current = @head | ||
|
||
if index >= self.length | ||
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. 💯 |
||
return nil | ||
end | ||
|
||
count = 0 | ||
|
||
until count == index | ||
count += 1 | ||
current = current.next | ||
end | ||
|
||
return current.data | ||
|
||
end | ||
|
||
def last_node | ||
current = @head | ||
while current.next != nil | ||
current = current.next | ||
end | ||
return current | ||
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.
👍
Could also be done as: