-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathAddTwoNumbers.java
56 lines (51 loc) · 1.5 KB
/
AddTwoNumbers.java
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
50
51
52
53
54
55
56
/*
* You have two numbers represented by a linked list, where each node
* contains a single digit. The digits are stored in reverse order,
* such that the 1's digit is at the head of the list. Write a
* function that adds the two numbers and returns the sum as a linked
* list.
Example
Given 7->1->6 + 5->9->2. That is, 617 + 295.
Return 2->1->9. That is 912.
Given 3->1->5 and 5->9->2, return 8->0->8.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class AddTwoNumbers {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addList(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
int carrier = 0;
while (l1 != null || l2 != null) {
int v1 = l1 != null ? l1.val : 0;
int v2 = l2 != null ? l2.val : 0;
cur.next = new ListNode((v1 + v2 + carrier) % 10);
cur = cur.next;
if (l1 != null) {
l1 = l1.next;
}
if (l2 != null) {
l2 = l2.next;
}
carrier = (v1 + v2 + carrier) / 10;
}
if (carrier != 0) {
cur.next = new ListNode(carrier);
}
return dummy.next;
}
}