-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.add-two-numbers.java
43 lines (41 loc) · 1.03 KB
/
2.add-two-numbers.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
/*
* @lc app=leetcode id=2 lang=java
*
* [2] Add Two Numbers
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, tmp = dummyHead;
// ListNode tmp = dummyHead;
int carry = 0;
while(p != null || q != null) {
int l1_val = (p != null) ? p.val : 0;
int l2_val = (q != null) ? q.val : 0;
int sum = carry + l1_val + l2_val;
carry = sum / 10;
tmp.next = new ListNode(sum % 10);
tmp = tmp.next;
if(p != null) {
p = p.next;
}
if(q != null) {
q = q.next;
}
}
if(carry > 0) {
tmp.next = new ListNode(carry);
}
return dummyHead.next;
}
}
// @lc code=end