You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md
+87-11Lines changed: 87 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -10,33 +10,46 @@ description: "This is a solution to the Add Two Numbers problem on LeetCode."
10
10
---
11
11
## Problem Description
12
12
13
-
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
13
+
| Problem Statement | Solution Link | LeetCode Profile |
|[Add Two Numbers on LeetCode](https://leetcode.com/problems/add-two-numbers/)|[Add Two Numbers Solution on LeetCode](https://leetcode.com/problems/add-two-numbers/solutions/5234194/solution/)|[Amruta Jayanti](https://leetcode.com/u/user7669cY/)|
16
+
17
+
18
+
## Problem Description
19
+
20
+
You are given two `non-empty` linked lists representing two non-negative integers. The digits are stored in `reverse order`, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
14
21
15
22
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
16
23
17
24
18
25
19
-
Example 1:
20
-
26
+
**Example 1:**
21
27
28
+
```plaintext
22
29
Input: l1 = [2,4,3], l2 = [5,6,4]
23
30
Output: [7,0,8]
24
31
Explanation: 342 + 465 = 807.
25
-
Example 2:
32
+
```
33
+
34
+
**Example 2:**
26
35
36
+
```plaintext
27
37
Input: l1 = [0], l2 = [0]
28
38
Output: [0]
29
-
Example 3:
39
+
```
40
+
41
+
**Example 3:**
30
42
43
+
```plaintext
31
44
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
32
45
Output: [8,9,9,9,0,0,0,1]
33
-
46
+
```
34
47
35
-
Constraints:
48
+
### Constraints:
36
49
37
-
The number of nodes in each linked list is in the range [1, 100].
38
-
0 <= Node.val <= 9
39
-
It is guaranteed that the list represents a number that does not have leading zeros.
50
+
-The number of nodes in each linked list is in the range `[1, 100]`.
51
+
-`0 <= Node.val <= 9`
52
+
-It is guaranteed that the list represents a number that does not have leading zeros.
40
53
41
54
42
55
## Solution to the problem
@@ -86,9 +99,72 @@ class Solution(object):
86
99
Above is the implementation in Python. Here total_sum stores the value and adds to the dummy. Variable carry is used to handle the carry bits.
87
100
88
101
#### Complexity Analysis:
89
-
- Time Complexity : $$O(max(n,m))$$.Here, n,m are the lengths of the input linked lists
102
+
- Time Complexity : $$O(max(n,m))$$ Here, n,m are the lengths of the input linked lists
0 commit comments