-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbers.java
78 lines (72 loc) · 1.78 KB
/
AddTwoNumbers.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
*/
public class AddTwoNumbers {
public static class ListNode {
int val;
ListNode next;
public ListNode(int x) { val = x; }
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int adds = 0;
ListNode root = new ListNode(0);
ListNode p = root;
while(l1 != null || l2!= null){
int a = 0;
int b = 0;
if(l1!= null){
a = l1.val;
l1 = l1.next;
}
if(l2!= null){
b = l2.val;
l2 = l2.next;
}
int sum = a + b + adds;
adds = sum /10;
p.next = new ListNode(sum%10);
p = p.next;
}
if(adds != 0){
p.next = new ListNode(adds);
}
return root.next;
}
public static void main(String[] args) {
ListNode node1 = new ListNode(3);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(7);
ListNode node4 = new ListNode(1);
ListNode node5 = new ListNode(9);
ListNode node6 = new ListNode(6);
node1.next = node2;
node2.next = node3;
node3.next = null;
node4.next = node5;
node5.next = node6;
node6.next = null;
System.out.print( "Before: ");
System.out.println();
System.out.print( "List 1: ");
ListNode curr = node1;
while(curr != null){
System.out.print( curr.val + " ");
curr = curr.next;
}
System.out.println();
System.out.print( "List 2: ");
curr = node4;
while(curr != null){
System.out.print( curr.val + " ");
curr = curr.next;
}
System.out.println();
System.out.print("After: ");
ListNode newHead = addTwoNumbers(node1, node4);
curr = newHead;
while(curr != null){
System.out.print( curr.val + " ");
curr = curr.next;
}
return;
}
}