-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListSum.java
More file actions
72 lines (61 loc) · 1.34 KB
/
LinkedListSum.java
File metadata and controls
72 lines (61 loc) · 1.34 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
65
66
67
68
69
70
71
72
//Sum Two numbers represented by linked list. Assumed that each number has same number of digits
class Node{
int val;
Node next;
Node(int val){
this.val = val;
this.next = null;
}
}
public class LinkedListSum{
public static Node add(Node h1, Node h2){
Node result = null;
if(h1.next == null && h2.next == null){
int val = h1.val + h2.val;
if(val < 10){
Node temp = new Node(0);
Node temp1 = new Node(val);
temp.next = temp1;
return temp;
}
else{
int carry = val/10;
int num = val%10;
Node temp = new Node(carry);
temp.next = new Node(num);
return temp;
}
}
else{
int val = h1.val + h2.val;
Node s = add(h1.next, h2.next);
val = val + s.val;
if(val < 10){
result = new Node(val);
result.next = s.next;
}
else{
result = new Node(val/10);
result.next = new Node(val%10);
result.next.next = s.next;
}
}
return result;
}
public static void main(String[] args){
Node head1 = new Node(3);
head1.next = new Node(4);
head1.next.next = new Node(8);
head1.next.next.next = new Node(5);
Node head2 = new Node(5);
head2.next = new Node(7);
head2.next.next = new Node(9);
head2.next.next.next = new Node(1);
Node sum = add(head1, head2);
Node temp = sum;
while(temp != null){
System.out.print(temp.val);
temp = temp.next;
}
}
}