-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdd two LinkedList
More file actions
30 lines (28 loc) · 920 Bytes
/
Add two LinkedList
File metadata and controls
30 lines (28 loc) · 920 Bytes
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
public static int addlisthelp(Node one,int pv1,Node two,int pv2,LinkedList res){
if(one==null && two==null){
return 0;
}
int data =0;
if(pv1>pv2){
int oc = addlisthelp(one.next,pv1-1,two,pv2,res);
data = one.data+oc;
}else if(pv1<pv2){
int oc = addlisthelp(one,pv1,two.next,pv2-1,res);
data = two.data+oc;
}else{
int oc = addlisthelp(one.next,pv1-1,two.next,pv2-1,res);
data = one.data+two.data+oc;
}
int nd = data%10;
int nc = data/10;
res.addFirst(nd);
return nc;
}
public static LinkedList addTwoLists(LinkedList one, LinkedList two) {
LinkedList res = new LinkedList();
int oc = addlisthelp(one.head,one.size,two.head,two.size,res);
if(oc>0){
res.addFirst(oc);
}
return res;
}