Skip to content

Commit

Permalink
feat: update solutions to leetcode problem: No.0002
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Feb 12, 2021
1 parent 54258df commit a025ba4
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 74 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
- [数组中重复的数字](/lcof/面试题03.%20数组中重复的数字/README.md)
- [旋转数组](/solution/0100-0199/0189.Rotate%20Array/README.md)
- [螺旋矩阵](/solution/0000-0099/0054.Spiral%20Matrix/README.md)
- [两数之和](/solution/0000-0099/0001.Two%20Sum/README.md)
- [三数之和](/solution/0000-0099/0015.3Sum/README.md)
- [四数之和](/solution/0000-0099/0018.4Sum/README.md)
- [合并两个有序数组](/solution/0000-0099/0088.Merge%20Sorted%20Array/README.md)
Expand All @@ -73,6 +74,7 @@

### 链表

- [两数相加](/solution/0000-0099/0002.Add%20Two%20Numbers/README.md)
- [从尾到头打印链表](/lcof/面试题06.%20从尾到头打印链表/README.md)
- [删除链表的节点](/lcof/面试题18.%20删除链表的节点/README.md)
- [移除链表元素](/solution/0200-0299/0203.Remove%20Linked%20List%20Elements/README.md)
Expand Down
2 changes: 2 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
- [Move Zeroes](/solution/0200-0299/0283.Move%20Zeroes/README_EN.md)
- [Rotate Array](/solution/0100-0199/0189.Rotate%20Array/README_EN.md)
- [Spiral Matrix](/solution/0000-0099/0054.Spiral%20Matrix/README_EN.md)
- [Two Sum](/solution/0000-0099/0001.Two%20Sum/README_EN.md)
- [3Sum](/solution/0000-0099/0015.3Sum/README_EN.md)
- [4Sum](/solution/0000-0099/0018.4Sum/README_EN.md)
- [Merge Sorted Array](/solution/0000-0099/0088.Merge%20Sorted%20Array/README_EN.md)
Expand All @@ -72,6 +73,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF

### Linked List

- [Add Two Numbers](/solution/0000-0099/0002.Add%20Two%20Numbers/README_EN.md)
- [Delete Node in a Linked List](/solution/0200-0299/0237.Delete%20Node%20in%20a%20Linked%20List/README_EN.md)
- [Remove Linked List Elements](/solution/0200-0299/0203.Remove%20Linked%20List%20Elements/README_EN.md)
- [Kth Node From End of List](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README_EN.md)
Expand Down
77 changes: 76 additions & 1 deletion solution/0000-0099/0002.Add Two Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,90 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
carry = 0
dummy = ListNode(-1)
cur = dummy
while l1 or l2 or carry:
t = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
carry = t // 10
cur.next = ListNode(t % 10)
cur = cur.next
l1 = None if not l1 else l1.next
l2 = None if not l2 else l2.next
return dummy.next
```

### **Java**

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

```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (l1 != null || l2 != null || carry != 0) {
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
carry = t / 10;
cur.next = new ListNode(t % 10);
cur = cur.next;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
return dummy.next;
}
}
```

### **C#**

```cs
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
var carry = 0;
while (l1 != null || l2 != null || carry != 0)
{
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
carry = t / 10;
cur.next = new ListNode(t % 10);
cur = cur.next;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
return dummy.next;
}
}
```

### **...**
Expand Down
77 changes: 76 additions & 1 deletion solution/0000-0099/0002.Add Two Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,88 @@
### **Python3**

```python

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
carry = 0
dummy = ListNode(-1)
cur = dummy
while l1 or l2 or carry:
t = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
carry = t // 10
cur.next = ListNode(t % 10)
cur = cur.next
l1 = None if not l1 else l1.next
l2 = None if not l2 else l2.next
return dummy.next
```

### **Java**

```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (l1 != null || l2 != null || carry != 0) {
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
carry = t / 10;
cur.next = new ListNode(t % 10);
cur = cur.next;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
return dummy.next;
}
}
```

### **C#**

```cs
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
var carry = 0;
while (l1 != null || l2 != null || carry != 0)
{
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
carry = t / 10;
cur.next = new ListNode(t % 10);
cur = cur.next;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
return dummy.next;
}
}
```

### **...**
Expand Down
42 changes: 22 additions & 20 deletions solution/0000-0099/0002.Add Two Numbers/Solution.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
return AddInternal(l1, l2, false);
}

private ListNode AddInternal(ListNode l1, ListNode l2, bool plusOne)
{
if (l1 == null && l2 == null)
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
var carry = 0;
while (l1 != null || l2 != null || carry != 0)
{
if (plusOne)
{
return new ListNode(1);
}
return null;
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
carry = t / 10;
cur.next = new ListNode(t % 10);
cur = cur.next;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}

var val = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + (plusOne ? 1 : 0);
plusOne = val >= 10;
val %= 10;
return new ListNode(val)
{
//next = AddInternal(l1?.next, l2?.next, plusOne);
next = AddInternal(l1 == null ? null : l1.next, l2 == null ? null : l2.next, plusOne)
};
return dummy.next;
}
}
33 changes: 21 additions & 12 deletions solution/0000-0099/0002.Add Two Numbers/Solution.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode res = new ListNode(-1);
ListNode cur = res;
int quotient = 0;
while (l1 != null || l2 != null || quotient != 0) {
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + quotient;
quotient = t / 10;
ListNode node = new ListNode(t % 10);
cur.next = node;
cur = node;
l1 = (l1 == null) ? l1 : l1.next;
l2 = (l2 == null) ? l2 : l2.next;
int carry = 0;
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (l1 != null || l2 != null || carry != 0) {
int t = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
carry = t / 10;
cur.next = new ListNode(t % 10);
cur = cur.next;
l1 = l1 == null ? null : l1.next;
l2 = l2 == null ? null : l2.next;
}
return res.next;
return dummy.next;
}
}
55 changes: 15 additions & 40 deletions solution/0000-0099/0002.Add Two Numbers/Solution.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
ans=ListNode(-1)
mn=ans
tmp1=[]
while l1:
tmp1.append(l1.val)
l1=l1.next
tmp1.reverse()
l11=''
for i in tmp1:
l11+=str(i)
l11=int(l11)
tmp2=[]
while l2:
tmp2.append(l2.val)
l2=l2.next
tmp2.reverse()
l22=''
for i in tmp2:
l22+=str(i)
l22=int(l22)
tmp=l11+l22
tmp=str(tmp)
tmp3=[]
for i in tmp:
tmp3.append(i)
tmp3.reverse()
for j in tmp3:
ans.next=ListNode(int(j))
ans=ans.next
return mn.next
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
carry = 0
dummy = ListNode(-1)
cur = dummy
while l1 or l2 or carry:
t = (0 if not l1 else l1.val) + (0 if not l2 else l2.val) + carry
carry = t // 10
cur.next = ListNode(t % 10)
cur = cur.next
l1 = None if not l1 else l1.next
l2 = None if not l2 else l2.next
return dummy.next

0 comments on commit a025ba4

Please sign in to comment.