-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution0002.java
More file actions
38 lines (34 loc) · 1.12 KB
/
Copy pathSolution0002.java
File metadata and controls
38 lines (34 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 2. 两数相加
/**
* 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; }
* }
*/
/*
1、同时遍历两个链表,对应位置的值相加,且加上进位值,计算新的进位值,创建新的节点
2、两个链表长度不同,则短链表后面默认为0
3、链表遍历结束后,如果进位值为1,需要再加一个节点
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode cur = head;
int add = 0;
while (l1 != null || l2 != null || add != 0) {
int l1Val = l1 != null ? l1.val : 0;
int l2Val = l2 != null ? l2.val : 0;
int num = l1Val + l2Val + add;
add = num / 10;
cur.next = new ListNode(num % 10);
cur = cur.next;
l1 = l1 != null ? l1.next : null;
l2 = l2 != null ? l2.next : null;
}
return head.next;
}
}