neetcode-gh/leetcode

Leetcode 2. add-two-numbers

Opened this issue · 1 comments

In the problem 2. add-two-numbers, the iterative solution should have a time and space complexity of:

  • Time complexity: $O(N)$
  • Space complexity: $O(N)$

Because the iterations happen till the larger list i.e $N = max(L_1, L_2)$ and in every iteration, there is a new node being created at the line cur.next = ListNode(val) and so the space complexity should also be $O(N)$.

reference

In the problem 2. add-two-numbers, the iterative solution should have a time and space complexity of:

  • Time complexity:
    O
    (
    N
    )
  • Space complexity:
    O
    (
    N
    )

Because the iterations happen till the larger list i.e N = m a x ( L 1 , L 2 ) and in every iteration, there is a new node being created at the line cur.next = ListNode(val) and so the space complexity should also be O ( N ) .

reference

We don't count the output into auxiliary space complexity so the auxiliary space remains O(1). By the way thanks for the feedback, we will add "O(1) due to no extra space but O(n) total space" line into the complexity description for easy understanding.