Repository navigation
Expand file tree
/
Copy path_002AddTwoNumbers.java
More file actions
52 lines (48 loc) · 1.46 KB
/
Copy path_002AddTwoNumbers.java
File metadata and controls
52 lines (48 loc) · 1.46 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
/**
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
* <p>
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 0 -> 8
* <p>
* Created by drfish on 16/04/2017.
*/
/**
* Definition for singly-linked list.
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public class _002AddTwoNumbers {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode result = new ListNode(0);
ListNode cur = result;
while (l1 != null || l2 != null) {
cur.val += addTwoNodes(l1, l2);
if (cur.val >= 10) {
cur.val -= 10;
cur.next = new ListNode(1);
} else {
if (l1 != null && l1.next != null || l2 != null && l2.next != null)
cur.next = new ListNode(0);
}
cur = cur.next;
if (l1 != null)
l1 = l1.next;
if (l2 != null)
l2 = l2.next;
}
return result;
}
private int addTwoNodes(ListNode n1, ListNode n2) {
if (n1 == null)
return n2.val;
if (n2 == null)
return n1.val;
return n1.val + n2.val;
}
}