Skip to content

Commit 40403e0

Browse files
Add 'Add Two Numbers'
1 parent 5626eb0 commit 40403e0

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

Add Two Numbers/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Add Two Numbers
2+
3+
## LeetCode Problem
4+
- Problem Number: 2
5+
- Problem Link: https://leetcode.com/problems/add-two-numbers/
6+
- Difficulty: Medium
7+
- Topics: Linked List, Math, Recursion
8+
9+
## Problem Description
10+
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list.
11+
12+
You may assume:
13+
- The two numbers do not contain any leading zero, except the number 0 itself
14+
- Each linked list represents a valid number
15+
16+
## Examples
17+
18+
### Example 1:
19+
```
20+
Input: l1 = [2,4,3], l2 = [5,6,4]
21+
Output: [7,0,8]
22+
Explanation: 342 + 465 = 807
23+
```
24+
25+
### Example 2:
26+
```
27+
Input: l1 = [0], l2 = [0]
28+
Output: [0]
29+
```
30+
31+
### Example 3:
32+
```
33+
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
34+
Output: [8,9,9,9,0,0,0,1]
35+
```
36+
37+
## Constraints
38+
- The number of nodes in each linked list is in the range [1, 100]
39+
- 0 <= Node.val <= 9
40+
- It is guaranteed that the list represents a number that does not have leading zeros

Add Two Numbers/Solution.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
3+
ListNode dummyHead = new ListNode(0);
4+
ListNode current = dummyHead;
5+
6+
int carry = 0;
7+
8+
while (l1 != null || l2 != null || carry != 0) {
9+
int sum = carry;
10+
11+
if (l1 != null) {
12+
sum += l1.val;
13+
l1 = l1.next;
14+
}
15+
16+
if (l2 != null) {
17+
sum += l2.val;
18+
l2 = l2.next;
19+
}
20+
21+
carry = sum / 10;
22+
23+
current.next = new ListNode(sum % 10);
24+
current = current.next;
25+
}
26+
27+
return dummyHead.next;
28+
}
29+
}

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ Each problem has its own directory containing:
1313
| # | Title | Difficulty | Solution | Topics |
1414
|---|-------|------------|----------|---------|
1515
| 1 | [Two Sum](Two%20Sum) | Easy | [Java](Two%20Sum/Solution.java) | Array, Hash Table |
16+
| 2 | [Add Two Numbers](Add%20Two%20Numbers) | Medium | [Java](Add%20Two%20Numbers/Solution.java) | Linked List, Math, Recursion |
1617
| 4 | [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays) | Hard | [Java](Median%20of%20Two%20Sorted%20Arrays/Solution.java) | Array, Binary Search, Divide and Conquer |
1718
| 2924 | [Find Champion II](Find%20Champion%20II) | Medium | [Java](Find%20Champion%20II/Solution.java) | Graph |
1819

1920
## Categories
2021

2122
### By Difficulty
2223
- Easy: 1
23-
- Medium: 1
24+
- Medium: 2
2425
- Hard: 1
2526

2627
### By Topics
@@ -29,6 +30,10 @@ Each problem has its own directory containing:
2930
- [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays)
3031
- Hash Table Problems
3132
- [Two Sum](Two%20Sum)
33+
- Linked List Problems
34+
- [Add Two Numbers](Add%20Two%20Numbers)
35+
- Math Problems
36+
- [Add Two Numbers](Add%20Two%20Numbers)
3237
- Binary Search Problems
3338
- [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays)
3439
- Graph Problems

0 commit comments

Comments
 (0)