LeetCode2: 两数相加

# Code

• Python
``````"""

"""
from typing import Optional

class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
ret = ListNode(0)
curr = ret
carry = 0  # 用于获取计算结果多余的值,相加的余数
while l1 != None or l2 != None or carry != 0:  # TODO 注意判断条件是啥
l1Val = l1.val if l1 else 0
l2Val = l2.val if l2 else 0
print("l1Val:{},l2Val".format(l1Val, l2Val))
carry = (l1Val + l2Val + carry) // 10
newNode = ListNode((l1Val + l2Val + carry) % 10)
curr.next = newNode
curr = newNode
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None

if __name__ == '__main__':
S = Solution()
o1 = [2, 4, 3]
o2 = [5, 6, 4, 9]

l1 = ListNode(o1[0])
l1_curr = l1
for i in range(1, len(o1)):
newNode = ListNode(o1[i])
l1_curr.next = newNode
l1_curr = newNode

l2 = ListNode(o2[0])
l2_curr = l2
for i in range(1, len(o2)):
newNode = ListNode(o2[i])
l2_curr.next = newNode
l2_curr = newNode

print(ret)
while ret != None:
print("val:{}".format(ret.val))
ret = ret.next if ret else None
``````
• java
``````import java.util.LinkedList;

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) {
//        System.out.printf("l1: %s", l1);
//        System.out.printf("l2: %s", l2);
ListNode result = new ListNode();
ListNode current = result;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
int sum = carry;
if (l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.val;
l2 = l2.next;
}
carry = sum / 10;
current.next = new ListNode(sum % 10);
current = current.next;
}
return result.next;
}
}

public static void main(String[] args) {
//        ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
//        ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));

int count = 0;
while (count < 10) {
count++;
}

ListNode l1Node = new ListNode();
ListNode l1Current = l1Node;
for (int i = 0; i < l1.size(); i++) {
System.out.printf("l1: %d\t", l1.get(i));
l1Current.val = l1.get(i);
l1Current.next = new ListNode();
l1Current = l1Current.next;
}

System.out.println("");

ListNode l2Node = new ListNode();
ListNode l2Current = l2Node;
for (int i = 0; i < l2.size(); i++) {
System.out.printf("l2: %d\t", l2.get(i));
l2Current.val = l2.get(i);
l2Current.next = new ListNode();
l2Current = l2Current.next;
}

System.out.println("");

ListNode l3 = new Solution().addTwoNumbers(l1Node, l2Node);
while (l3 != null){
//            System.out.println(l3.val);
System.out.printf("l3: %d\t", l3.val);
l3 = l3.next;
}
}
}
``````

### 要不赞赏一下?

 微信 支付宝 PayPal Bitcoin

``https://www.emperinter.info/2022/11/28/add-two-numbers/``

 阿里云国际版 20美元 Vultr 10美元 搬瓦工 | Bandwagon 应该有折扣吧？ Just My Socks JMS9272283 【注意手动复制去跳转】 域名 | namesilo `emperinter`(1美元) 币安 币安