forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 30
/
code4java
174 lines (151 loc) · 3.88 KB
/
code4java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.*;
class GFG {
static class Node {
int data;
Node next;
};
// recursive function
static Node addition(Node temp1, Node temp2, int size1,
int size2)
{
// creating a new Node
Node newNode = new Node();
// base case
if (temp1 != null && temp2 != null
&& temp1.next == null && temp2.next == null) {
// addition of current nodes which is the last
// nodes of both linked lists
newNode.data = (temp1.data + temp2.data);
// set this current node's link null
newNode.next = null;
// return the current node
return newNode;
}
// creating a node that contains sum of previously
// added number
Node returnedNode = new Node();
// if sizes are same then we move in both linked
// list
if ((temp1 != null && temp2 != null)
&& size2 == size1) {
// recursively call the function
// move ahead in both linked list
returnedNode = addition(temp1.next, temp2.next,
size1 - 1, size2 - 1);
// add the current nodes and append the carry
newNode.data = (temp1.data + temp2.data)
+ ((returnedNode.data) / 10);
}
// or else we just move in big linked list
else if (temp1 != null && temp2 != null) {
// recursively call the function
// move ahead in big linked list
returnedNode = addition(temp1, temp2.next,
size1, size2 - 1);
// add the current node and carry
newNode.data
= (temp2.data) + ((returnedNode.data) / 10);
}
// this node contains previously added numbers
// so we need to set only rightmost digit of it
returnedNode.data = (returnedNode.data) % 10;
// set the returned node to the current node
newNode.next = returnedNode;
// return the current node
return newNode;
}
// Function to add two numbers represented by nexted
// list.
static Node addTwoLists(Node head1, Node head2)
{
Node temp1, temp2, ans = null;
temp1 = head1;
temp2 = head2;
int size1 = 0, size2 = 0;
// calculating the size of first linked list
while (temp1 != null) {
temp1 = temp1.next;
size1++;
}
// calculating the size of second linked list
while (temp2 != null) {
temp2 = temp2.next;
size2++;
}
Node returnedNode = new Node();
// traverse the bigger linked list
if (size2 > size1) {
returnedNode
= addition(head1, head2, size1, size2);
}
else {
returnedNode
= addition(head2, head1, size2, size1);
}
// creating new node if head node is >10
if (returnedNode.data >= 10) {
ans = new Node();
ans.data = (returnedNode.data) / 10;
returnedNode.data = returnedNode.data % 10;
ans.next = returnedNode;
}
else
ans = returnedNode;
// return the head node of linked list that contains
// answer
return ans;
}
static void Display(Node head)
{
if (head == null) {
return;
}
while (head.next != null) {
System.out.print(head.data + " -> ");
head = head.next;
}
System.out.print(head.data + "\n");
}
// Function that adds element at the end of the Linked
// List
static Node push(Node head_ref, int d)
{
Node new_node = new Node();
new_node.data = d;
new_node.next = null;
if (head_ref == null) {
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
Node last = head_ref;
while (last.next != null && last != null) {
last = last.next;
}
last.next = new_node;
return head_ref;
}
// Driver Program for above Functions
public static void main(String[] args)
{
// Creating two lists
Node first = null;
Node second = null;
Node sum = null;
first = push(first, 7);
first = push(first, 5);
first = push(first, 9);
first = push(first, 4);
first = push(first, 6);
second = push(second, 8);
second = push(second, 4);
System.out.print("First List : ");
Display(first);
System.out.print("Second List : ");
Display(second);
sum = addTwoLists(first, second);
System.out.print("Sum List : ");
Display(sum);
}
}
// This code contributed by shikhasingrajput