-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo2_AddTwoNumbers.java
More file actions
49 lines (46 loc) · 1.01 KB
/
No2_AddTwoNumbers.java
File metadata and controls
49 lines (46 loc) · 1.01 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
39
40
41
42
43
44
45
46
47
48
49
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode p1, p2, head, pLast;
int tmp = 0;
head = new ListNode(-1);
pLast = head;
for (p1 = l1, p2 = l2; p1 != null && p2 != null; p1 = p1.next, p2 = p2.next) {
pLast.next = new ListNode(0);
pLast = pLast.next;
int sum = p1.val + p2.val + tmp;
pLast.val = sum % 10;
tmp = sum / 10;
}
while (p1 != null) {
pLast.next = new ListNode(0);
pLast = pLast.next;
int sum = p1.val + tmp;
pLast.val = sum % 10;
tmp = sum / 10;
p1 = p1.next;
}
while (p2 != null) {
pLast.next = new ListNode(0);
pLast = pLast.next;
int sum = p2.val + tmp;
pLast.val = sum % 10;
tmp = sum / 10;
p2 = p2.next;
}
if (tmp != 0) {
pLast.next = new ListNode(0);
pLast = pLast.next;
pLast.val = tmp;
tmp = 0;
}
return head.next;
}
}