0

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);
    }
}

1 Answer 1

1

Before the compiler runs your code it runs its own main function with the line: ListNode ret = new Solution().addTwoNumbers(param_1, param_2); Which means that you don't have to implement the main, but a class Solution with a function addTwoNumbers(param_1, param_2), which get the appropriate parameters and returns the appropriate object.
In your problem (right?), the function should return ListNode object - which had been defined (you don't have to implemnt it), so your Node class is unnecessary.

You can see a detailed guide for the problem in the Solution tab.

Sign up to request clarification or add additional context in comments.

3 Comments

1)So i have to write Only the Function?No main function?You say that node class is unnecessary How can I create a linked list without making a node?
So you mean that I don't have to create a node.Nodes have been already created?
Yes, you don't need to implement the main function and the Node class. You can see in the default code outside the Solution class, an annotated class (in your case - ListNode) that you should use instead of the Node class. You can see the default code using the Reset to default code definition button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.