Skip to content
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

Leetcode 2. add-two-numbers #3752

Open
nsk126 opened this issue Nov 27, 2024 · 1 comment
Open

Leetcode 2. add-two-numbers #3752

nsk126 opened this issue Nov 27, 2024 · 1 comment

Comments

@nsk126
Copy link
Contributor

nsk126 commented Nov 27, 2024

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

@Srihari2222
Copy link
Collaborator

Srihari2222 commented Nov 30, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants