-
Notifications
You must be signed in to change notification settings - Fork 2
/
Add two Linked Lists.java
41 lines (35 loc) · 1.02 KB
/
Add two Linked Lists.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
public static int AddTwoHelp( Node one, int size1, Node two, int size2, LinkedList res)
{ int val=0;
if(one==null && two==null)
{
return 0;
}
if(size1>size2)
{
int oc=AddTwoHelp(one.next,size1-1,two,size2,res);
val=one.data+oc;
}
else if(size1<size2)
{
int oc=AddTwoHelp(one.next,size1,two.next,size2-1,res);
val=two.data+oc;
}
else
{
int oc=AddTwoHelp(one.next,size1-1,two.next,size2-1,res);
val=one.data+two.data+oc;
}
int og=val%10;
int adcar=val/10;
res.addFirst(og);
return adcar;
}
public static LinkedList addTwoLists(LinkedList one, LinkedList two) {
LinkedList res=new LinkedList();
int oc=AddTwoHelp(one.head,one.size,two.head,two.size,res);
if(oc>0)
{
res.addFirst(oc);
}
return res;
}