Skip to content

Commit a025ba4

Browse files
committed
feat: update solutions to leetcode problem: No.0002
1 parent 54258df commit a025ba4

File tree

7 files changed

+214
-74
lines changed

7 files changed

+214
-74
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- [数组中重复的数字](/lcof/面试题03.%20数组中重复的数字/README.md)
5959
- [旋转数组](/solution/0100-0199/0189.Rotate%20Array/README.md)
6060
- [螺旋矩阵](/solution/0000-0099/0054.Spiral%20Matrix/README.md)
61+
- [两数之和](/solution/0000-0099/0001.Two%20Sum/README.md)
6162
- [三数之和](/solution/0000-0099/0015.3Sum/README.md)
6263
- [四数之和](/solution/0000-0099/0018.4Sum/README.md)
6364
- [合并两个有序数组](/solution/0000-0099/0088.Merge%20Sorted%20Array/README.md)
@@ -73,6 +74,7 @@
7374

7475
### 链表
7576

77+
- [两数相加](/solution/0000-0099/0002.Add%20Two%20Numbers/README.md)
7678
- [从尾到头打印链表](/lcof/面试题06.%20从尾到头打印链表/README.md)
7779
- [删除链表的节点](/lcof/面试题18.%20删除链表的节点/README.md)
7880
- [移除链表元素](/solution/0200-0299/0203.Remove%20Linked%20List%20Elements/README.md)

README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
5757
- [Move Zeroes](/solution/0200-0299/0283.Move%20Zeroes/README_EN.md)
5858
- [Rotate Array](/solution/0100-0199/0189.Rotate%20Array/README_EN.md)
5959
- [Spiral Matrix](/solution/0000-0099/0054.Spiral%20Matrix/README_EN.md)
60+
- [Two Sum](/solution/0000-0099/0001.Two%20Sum/README_EN.md)
6061
- [3Sum](/solution/0000-0099/0015.3Sum/README_EN.md)
6162
- [4Sum](/solution/0000-0099/0018.4Sum/README_EN.md)
6263
- [Merge Sorted Array](/solution/0000-0099/0088.Merge%20Sorted%20Array/README_EN.md)
@@ -72,6 +73,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
7273

7374
### Linked List
7475

76+
- [Add Two Numbers](/solution/0000-0099/0002.Add%20Two%20Numbers/README_EN.md)
7577
- [Delete Node in a Linked List](/solution/0200-0299/0237.Delete%20Node%20in%20a%20Linked%20List/README_EN.md)
7678
- [Remove Linked List Elements](/solution/0200-0299/0203.Remove%20Linked%20List%20Elements/README_EN.md)
7779
- [Kth Node From End of List](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README_EN.md)

solution/0000-0099/0002.Add Two Numbers/README.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,90 @@
2929
<!-- 这里可写当前语言的特殊实现逻辑 -->
3030

3131
```python
32-
32+
# Definition for singly-linked list.
33+
# class ListNode:
34+
# def __init__(self, val=0, next=None):
35+
# self.val = val
36+
# self.next = next
37+
class Solution:
38+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
39+
carry = 0
40+
dummy = ListNode(-1)
41+
cur = dummy
42+
while l1 or l2 or carry:
43+
t = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
44+
carry = t // 10
45+
cur.next = ListNode(t % 10)
46+
cur = cur.next
47+
l1 = None if not l1 else l1.next
48+
l2 = None if not l2 else l2.next
49+
return dummy.next
3350
```
3451

3552
### **Java**
3653

3754
<!-- 这里可写当前语言的特殊实现逻辑 -->
3855

3956
```java
57+
/**
58+
* Definition for singly-linked list.
59+
* public class ListNode {
60+
* int val;
61+
* ListNode next;
62+
* ListNode() {}
63+
* ListNode(int val) { this.val = val; }
64+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
65+
* }
66+
*/
67+
class Solution {
68+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
69+
int carry = 0;
70+
ListNode dummy = new ListNode(-1);
71+
ListNode cur = dummy;
72+
while (l1 != null || l2 != null || carry != 0) {
73+
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
74+
carry = t / 10;
75+
cur.next = new ListNode(t % 10);
76+
cur = cur.next;
77+
l1 = l1 == null ? null : l1.next;
78+
l2 = l2 == null ? null : l2.next;
79+
}
80+
return dummy.next;
81+
}
82+
}
83+
```
4084

85+
### **C#**
86+
87+
```cs
88+
/**
89+
* Definition for singly-linked list.
90+
* public class ListNode {
91+
* public int val;
92+
* public ListNode next;
93+
* public ListNode(int val=0, ListNode next=null) {
94+
* this.val = val;
95+
* this.next = next;
96+
* }
97+
* }
98+
*/
99+
public class Solution {
100+
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
101+
ListNode dummy = new ListNode(-1);
102+
ListNode cur = dummy;
103+
var carry = 0;
104+
while (l1 != null || l2 != null || carry != 0)
105+
{
106+
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
107+
carry = t / 10;
108+
cur.next = new ListNode(t % 10);
109+
cur = cur.next;
110+
l1 = l1 == null ? null : l1.next;
111+
l2 = l2 == null ? null : l2.next;
112+
}
113+
return dummy.next;
114+
}
115+
}
41116
```
42117

43118
### **...**

solution/0000-0099/0002.Add Two Numbers/README_EN.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,88 @@
2727
### **Python3**
2828

2929
```python
30-
30+
# Definition for singly-linked list.
31+
# class ListNode:
32+
# def __init__(self, val=0, next=None):
33+
# self.val = val
34+
# self.next = next
35+
class Solution:
36+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
37+
carry = 0
38+
dummy = ListNode(-1)
39+
cur = dummy
40+
while l1 or l2 or carry:
41+
t = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
42+
carry = t // 10
43+
cur.next = ListNode(t % 10)
44+
cur = cur.next
45+
l1 = None if not l1 else l1.next
46+
l2 = None if not l2 else l2.next
47+
return dummy.next
3148
```
3249

3350
### **Java**
3451

3552
```java
53+
/**
54+
* Definition for singly-linked list.
55+
* public class ListNode {
56+
* int val;
57+
* ListNode next;
58+
* ListNode() {}
59+
* ListNode(int val) { this.val = val; }
60+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
61+
* }
62+
*/
63+
class Solution {
64+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
65+
int carry = 0;
66+
ListNode dummy = new ListNode(-1);
67+
ListNode cur = dummy;
68+
while (l1 != null || l2 != null || carry != 0) {
69+
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
70+
carry = t / 10;
71+
cur.next = new ListNode(t % 10);
72+
cur = cur.next;
73+
l1 = l1 == null ? null : l1.next;
74+
l2 = l2 == null ? null : l2.next;
75+
}
76+
return dummy.next;
77+
}
78+
}
79+
```
3680

81+
### **C#**
82+
83+
```cs
84+
/**
85+
* Definition for singly-linked list.
86+
* public class ListNode {
87+
* public int val;
88+
* public ListNode next;
89+
* public ListNode(int val=0, ListNode next=null) {
90+
* this.val = val;
91+
* this.next = next;
92+
* }
93+
* }
94+
*/
95+
public class Solution {
96+
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
97+
ListNode dummy = new ListNode(-1);
98+
ListNode cur = dummy;
99+
var carry = 0;
100+
while (l1 != null || l2 != null || carry != 0)
101+
{
102+
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
103+
carry = t / 10;
104+
cur.next = new ListNode(t % 10);
105+
cur = cur.next;
106+
l1 = l1 == null ? null : l1.next;
107+
l2 = l2 == null ? null : l2.next;
108+
}
109+
return dummy.next;
110+
}
111+
}
37112
```
38113

39114
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
112
public class Solution {
213
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
3-
return AddInternal(l1, l2, false);
4-
}
5-
6-
private ListNode AddInternal(ListNode l1, ListNode l2, bool plusOne)
7-
{
8-
if (l1 == null && l2 == null)
14+
ListNode dummy = new ListNode(-1);
15+
ListNode cur = dummy;
16+
var carry = 0;
17+
while (l1 != null || l2 != null || carry != 0)
918
{
10-
if (plusOne)
11-
{
12-
return new ListNode(1);
13-
}
14-
return null;
19+
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
20+
carry = t / 10;
21+
cur.next = new ListNode(t % 10);
22+
cur = cur.next;
23+
l1 = l1 == null ? null : l1.next;
24+
l2 = l2 == null ? null : l2.next;
1525
}
16-
17-
var val = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + (plusOne ? 1 : 0);
18-
plusOne = val >= 10;
19-
val %= 10;
20-
return new ListNode(val)
21-
{
22-
//next = AddInternal(l1?.next, l2?.next, plusOne);
23-
next = AddInternal(l1 == null ? null : l1.next, l2 == null ? null : l2.next, plusOne)
24-
};
26+
return dummy.next;
2527
}
2628
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
111
class Solution {
212
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
3-
ListNode res = new ListNode(-1);
4-
ListNode cur = res;
5-
int quotient = 0;
6-
while (l1 != null || l2 != null || quotient != 0) {
7-
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + quotient;
8-
quotient = t / 10;
9-
ListNode node = new ListNode(t % 10);
10-
cur.next = node;
11-
cur = node;
12-
l1 = (l1 == null) ? l1 : l1.next;
13-
l2 = (l2 == null) ? l2 : l2.next;
13+
int carry = 0;
14+
ListNode dummy = new ListNode(-1);
15+
ListNode cur = dummy;
16+
while (l1 != null || l2 != null || carry != 0) {
17+
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
18+
carry = t / 10;
19+
cur.next = new ListNode(t % 10);
20+
cur = cur.next;
21+
l1 = l1 == null ? null : l1.next;
22+
l2 = l2 == null ? null : l2.next;
1423
}
15-
return res.next;
24+
return dummy.next;
1625
}
1726
}
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,18 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
6-
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
76
class Solution:
8-
def addTwoNumbers(self, l1, l2):
9-
"""
10-
:type l1: ListNode
11-
:type l2: ListNode
12-
:rtype: ListNode
13-
"""
14-
ans=ListNode(-1)
15-
mn=ans
16-
tmp1=[]
17-
while l1:
18-
tmp1.append(l1.val)
19-
l1=l1.next
20-
tmp1.reverse()
21-
l11=''
22-
for i in tmp1:
23-
l11+=str(i)
24-
l11=int(l11)
25-
tmp2=[]
26-
while l2:
27-
tmp2.append(l2.val)
28-
l2=l2.next
29-
tmp2.reverse()
30-
l22=''
31-
for i in tmp2:
32-
l22+=str(i)
33-
l22=int(l22)
34-
tmp=l11+l22
35-
tmp=str(tmp)
36-
tmp3=[]
37-
for i in tmp:
38-
tmp3.append(i)
39-
tmp3.reverse()
40-
for j in tmp3:
41-
ans.next=ListNode(int(j))
42-
ans=ans.next
43-
return mn.next
7+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
8+
carry = 0
9+
dummy = ListNode(-1)
10+
cur = dummy
11+
while l1 or l2 or carry:
12+
t = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
13+
carry = t // 10
14+
cur.next = ListNode(t % 10)
15+
cur = cur.next
16+
l1 = None if not l1 else l1.next
17+
l2 = None if not l2 else l2.next
18+
return dummy.next

0 commit comments

Comments
 (0)