两数相加 Add Two Numbers [M]
题目:
https://leetcode.cn/problems/add-two-numbers/description/?favorite=2cktkvj
讲解
https://www.youtube.com/watch?v=wgFPrzTjm7s&ab_channel=NeetCode
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode();
ListNode cur = dummy;
int carry = 0;
while(l1 != null || l2 != null || carry !=0){
int v1 = l1 != null ? l1.val : 0;
int v2 = l2 != null ? l2.val : 0;
int v = v1 + v2 + carry;
carry = v / 10;
v %= 10;
cur.next = new ListNode(v);
cur = cur.next;
if(l1 != null)l1 = l1.next;
if(l2 != null)l2 = l2.next;
}
return dummy.next;
}
标签:ListNode,int,Two,next,Add,l2,Numbers,l1,null
From: https://www.cnblogs.com/jay-lon/p/17330209.html