-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd Two Numbers
More file actions
38 lines (38 loc) · 1.15 KB
/
Add Two Numbers
File metadata and controls
38 lines (38 loc) · 1.15 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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode t1 = l1, t2 = l2, start = null, current = null;
int carry = 0;
while(t1 != null || t2 != null || carry != 0)
{
int num1 = t1 == null ? 0 : t1.val;
int num2 = t2 == null ? 0 : t2.val;
int temp = (num1+num2+carry)%10;
carry = (num1+num2+carry)/10;
if(start == null)
{
start = new ListNode(temp);
current = start;
}//end if
else{
current.next = new ListNode(temp);
current = current.next;
}//end else
t1 = t1 == null ? null : t1.next;
t2 = t2 == null ? null : t2.next;
}//end while
return start;
}
}