-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path002.Add Two Numbers
More file actions
61 lines (55 loc) · 1.47 KB
/
Copy path002.Add Two Numbers
File metadata and controls
61 lines (55 loc) · 1.47 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
50
51
52
53
54
55
56
57
58
59
60
61
/**
* 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) {
int len1 = 0, len2 = 0;
ListNode temp1 = l1,temp2 = l2;
while(temp1 != null){
len1++;
temp1 = temp1.next;
}
while(temp2 != null){
len2++;
temp2 = temp2.next;
}
int len = len1 > len2 ? len1 : len2;
int[] num1 = new int[len];
int[] num2 = new int[len];
int[] numRes = new int[len+1];
int index1 = 0, index2 = 0;
temp1 = l1;
temp2 = l2;
while(temp1 != null){
num1[index1++] = temp1.val;
temp1 = temp1.next;
}
while(temp2 != null){
num2[index2++] = temp2.val;
temp2 = temp2.next;
}
for(int i = 0; i < len; i++){
numRes[i] += num1[i] + num2[i];
numRes[i+1] += numRes[i] / 10;
numRes[i] %= 10;
}
ListNode res = new ListNode(numRes[0]);
ListNode p = res;
int end = 1;
if(numRes[len] != 0){
len++;
}
while(end < len){
ListNode t = new ListNode(numRes[end]);
p.next = t;
p = t;
end++;
}
return res;
}
}