-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
64 lines (52 loc) · 1.7 KB
/
AddTwoNumbers.java
File metadata and controls
64 lines (52 loc) · 1.7 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
62
63
64
import java.util.*;
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dhead = new ListNode(0);
ListNode l3 = dhead;
int carry = 0;
while (l1 != null || l2 != null) {
int l1val = (l1 != null) ? l1.val : 0;
int l2val = (l2 != null) ? l2.val : 0;
int currsum = l1val + l2val + carry;
carry = currsum / 10;
int lastdigit = currsum % 10;
ListNode newnode = new ListNode(lastdigit);
l3.next = newnode;
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
l3 = l3.next;
}
if (carry > 0) {
ListNode newnode = new ListNode(carry);
l3.next = newnode;
}
return dhead.next;
}
public void printList(ListNode head) {
System.out.print("[");
while (head != null) {
System.out.print(head.val);
if (head.next != null) {
System.out.print(", ");
}
head = head.next;
}
System.out.println("]");
}
public static void main (String[] args){
Solution solution = new Solution();
ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
solution.printList(l1);
solution.printList(l2);
ListNode result = solution.addTwoNumbers(l1, l2);
solution.printList(result);
}
}