I am fairly new to java(About a month) and in fact new to coding.I was going through a question on leetcode.I made a code for it and it ran perfectly fine in vscode.But on online editor of leetcode where i have to submit the answer it shows compile time error.Can anyone help?Also can anyone guide me if they see this code as too lengthy??I did this while i am studying linked lists.Below is the code...
public class linkedlistcreation {
public static class Node{
int data;
Node next;
public Node(int data){
this.data=data;
this.next=null;
}
}
public static void display(Node firstnode){
for(Node x=firstnode;x!=null;x=x.next){
System.out.print(x.data + "=>");
}
}
//method to compare their lengths.
public static void compare(Node newfirst,Node root){
int count =0;
int j=0;
Node y;
Node x;
for(x=newfirst;x!=null;x=x.next){
count=count+1;
}
for( y=root;y!=null;y=y.next){
j=j+1;
}
if(count==j){
return;
}
if(count>j){
Node last=new Node(0);
while(j<count){
y.next=last;
j++;
}
return;
}
if(count<j){
Node last=new Node(0);
while(count<j){
x.next=last;
count++;
}
return;
}
}
public static void add(int z,Node firstnode,Node root){
if(firstnode==null && root==null){
return;
}
Node x=firstnode;
Node y=root;
x.data=z+x.data+y.data;
if(x.data>=10){
x.data=(x.data-10);
if(x.next==null&& y.next==null){
Node last=new Node(0);
x.next=last;
last.data=1;
return;
}
add(1, x.next, y.next);
}
else{
add(0, x.next, y.next);
}
}
public static void main(String[]args){
//first list // number passed in it is 6=>4=>3=>5=>6 so as per question we have forst operand as 65346
Node first=new Node(4);
Node second=new Node(3);
Node third=new Node(5);
Node fourth=new Node(6);
first.next=second;
second.next=third;
third.next=fourth;
//adding a new node at the first node.Just for checking my understanding.
Node newfirst=new Node(6);
newfirst.next=first;
//second list// second operand is 84467
Node root=new Node(7);
Node firs=new Node(6);
Node secon=new Node(4);
Node thir=new Node(4);
Node four=new Node(8);
root.next=firs;
firs.next=secon;
secon.next=thir;
thir.next=four;
compare(newfirst,root);
add(0,newfirst,root);
display(newfirst);
}
}