diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 9e97557..3811088 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -7,39 +7,74 @@ def initialize @head = nil end - # Time complexity - ? - # Space complexity - ? + # Time complexity - 0(1) + # Space complexity - 0(1) def add_first(data) - + @head = Node.new(data, @head) end - # Time complexity - ? - # Space complexity - ? + # Time complexity - 0(1) + # Space complexity - 0(1) def get_first - + + if head + return head.data + else + return nil + end end - # Time complexity - ? - # Space complexity - ? + # Time complexity - 0(n) + # Space complexity - 0(1) def length - return 0 + + current = head + length = 0 + while current != nil + length += 1 + current = current.next + end + return length end - # Time complexity - ? + # Time complexity - 0(n) # Space complexity - ? def add_last(data) - end + current = head + if current == nil + self.add_first(data) + return + end + while current.next != nil + current = current.next + end + current.next = Node.new(data) + end # Time complexity - ? # Space complexity - ? def get_last + current = head + while current.next != nil + current = current.next + end + return current.data + end + - end # Time complexity - ? # Space complexity - ? def get_at_index(index) + current = head + count = 0 + return nil if current.nil? + while count != index + count += 1 + current = current.next + end + return current.data end -end +end \ No newline at end of file