-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddTwoNumbers.java
132 lines (126 loc) · 3.21 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.util.LinkedList;
public class AddTwoNumbers {
public static void main(String[] args) {
LinkedList<Integer> l1 = new LinkedList<Integer>();
// l1.add(1);
l1.add(2);
// l1.add(4);
// l1.add(3);
LinkedList<Integer> l2 = new LinkedList<Integer>();
l2.add(9);
// l2.add(6);
// l2.add(4);
LinkedList<Integer> l3 = new LinkedList<Integer>();
l3 = new AddTwoNumbers().add(l1,l2);
System.out.println(l3);
}
public LinkedList<Integer> add(LinkedList<Integer> l1,LinkedList<Integer> l2) {
LinkedList<Integer> result = new LinkedList<Integer>();
int carry = 0;
/*
* if l1 & l2 lengths are same
* l1 = 1,2,3
* l2 = 4,5,6
*/
while(l1.iterator().hasNext() && l2.iterator().hasNext()) {
// System.out.println(l1.pop());
// System.out.println(l2.pop());
int num = ((int)l1.pop() + (int)l2.pop());
int x = num % 10;
x = x + carry;
carry = num/10;
result.add(x);
//System.out.println(x);
}
/*
* if l1 length is more
* l1 = 1,2
* l2 = 9
*/
while(l1.iterator().hasNext()) {
int num = (int)l1.pop();
int x = num % 10;
x = x + carry;
carry = num/10;
result.add(x);
}
/*
* if l2 length is more
* l1 = 9
* l2 = 12
*/
while(l2.iterator().hasNext()) {
int num = (int)l2.pop();
int x = num % 10;
x = x + carry;
carry = num/10;
result.add(x);
}
/*
* if carry remains
* l1 = 2
* l2 = 9
*/
if(carry != 0) {
result.add(carry);
}
return result;
/*
* Solution for leetcode
*/
/**
* 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) {
/*
* dummy node is for accessing the head of result node
*/
// ListNode dummy = new ListNode(0);
// ListNode result = dummy;
// int carry = 0;
// while(l1 != null && l2 != null){
// int x = l1.val + l2.val + carry;
// int num = x % 10;
// carry = x / 10;
// ListNode newNode = new ListNode(num); // create the newnode
// result.next = newNode; // link the newnode to the result link
// result = newNode; // move the head to the newnode
// l1 = l1.next;
// l2 = l2.next;
// }
// while(l1 != null){
// int x = l1.val + carry;
// int num = x % 10;
// carry = x / 10;
// ListNode newNode = new ListNode(num);
// result.next = newNode;
// result = newNode;
// l1 = l1.next;
// }
// while(l2 != null){
// int x = l2.val + carry;
// int num = x % 10;
// carry = x / 10;
// ListNode newNode = new ListNode(num);
// result.next = newNode;
// result = newNode;
// l2 = l2.next;
// }
// if(carry != 0){
// ListNode newNode = new ListNode(carry);
// result.next = newNode;
// result = newNode;
// }
//
//
// return dummy.next;
// }
// }
}
}