-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathSolution.java
44 lines (40 loc) · 1.09 KB
/
Solution.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
package com.blankj.medium._002;
import com.blankj.structure.ListNode;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/10/11
* desc :
* </pre>
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode p = l1, q = l2, curr = dummy;
int c = 0;
while(p!=null || q!=null){
int x = (p!=null)?p.val:0;
int y = (q!=null)?q.val:0;
int sum = x+y+c;
c = sum/10;
curr.next = new ListNode(sum%10);
curr = curr.next;
if(p!=null)
p=p.next;
if(q!=null)
q=q.next;
}
if(c>0){
curr.next = new ListNode(c);
}
return dummy.next;
}
public static void main(String[] args) {
Solution solution = new Solution();
ListNode.print(solution.addTwoNumbers(
ListNode.createTestData("[2,4,3]"),
ListNode.createTestData("[5,6,4]")
));
}
}