Add two numbers represented by linked lists I've just encountered this question and am trying to solve it using Java.

Here is my solution to it, which may not be optimized or might not be right way to do it. Someone please review whether it is correct or there is some good way to do it. For focusing on logic, I have hard coded List creation and code repetition is present.

package linkedlist.singly;

//Add two numbers represented by linked lists 
// 245   :  5 -&gt; 4 -&gt; 2
// 99789 :  9 -&gt; 8 -&gt; 7 -&gt; 9 -&gt; 9
// Ans   :  99341
public class Add2NumbersInLinkListType2 {

    static int carry=0;

    public static void main(String[] args) {

        Node templ11 = new Node(5);
        Node templ12 = new Node(4);
        Node templ13 = new Node(2);

        Node templ21 = new Node(9);
        Node templ22 = new Node(8);
        Node templ23 = new Node(7);
        Node templ24 = new Node(9);
        Node templ25 = new Node(9);

        templ11.setNext(templ12);
        templ12.setNext(templ13);

        templ21.setNext(templ22);
        templ22.setNext(templ23);
        templ23.setNext(templ24);
        templ24.setNext(templ25);

        Node res = findSum(templ11, templ21, 0);
        if(carry==1){
            Node tempNode = new Node(carry);
            tempNode.setNext(res);
            res = tempNode;
        }
        while(res!=null){
            System.out.print(res.getData());
            res=res.getNext();
        }
    }

    private static Node findSum(Node l1, Node l2, int diff){

        int length1 = findLength(l1);
        int length2 = findLength(l2);

        if(length1&gt;length2){
            //l1 having more nodes
            Node res = findSum(l1.getNext(), l2, diff--);
            int data = l1.getData() + carry;
            if(data&gt;9){
                carry=1;
                Node tempNode = new Node(data%10);
                tempNode.setNext(res);
                res = tempNode;
            }else{
                carry=0;
                Node tempNode = new Node(data);
                tempNode.setNext(res);
                res = tempNode;
            }
            return res;

        }else if(length2&gt;length1){
            //l2 having more nodes
            Node res = findSum(l1, l2.getNext(), diff++);
            int data = l2.getData() + carry;
            if(data&gt;9){
                carry=1;
                Node tempNode = new Node(data%10);
                tempNode.setNext(res);
                res = tempNode;
            }else{
                carry=0;
                Node tempNode = new Node(data);
                tempNode.setNext(res);
                res = tempNode;
            }
            return res;

        }else{
            //both have same length
            Node res = findSumForListOfSameSize(l1, l2);
            return res;
        }
    }

    private static Node findSumForListOfSameSize(Node l1, Node l2){
        if(l1==null &amp;&amp; l2==null)
            return null;

        Node head = findSumForListOfSameSize(l1.getNext(), l2.getNext());

        int temp = l1.getData() + l2.getData() + carry;
        if(temp&gt;9){
            carry=1;
        }else{
            carry=0;
        }

        if(head==null){
            head = new Node(temp % 10);
        }else{
            Node tempNode = new Node(temp % 10);
            tempNode.setNext(head);
            head = tempNode;
        }
        return head;
    }

    private static int findLength(Node node){
        int count=0;
        while(node!=null){
            count++;
            node = node.getNext();
        }
        return count;
    }
}
few notes on your implementation:

if(data&gt;9){
            carry=1;
            Node tempNode = new Node(data%10);
            tempNode.setNext(res);
            res = tempNode;
        }else{
            carry=0;
            Node tempNode = new Node(data);
            tempNode.setNext(res);
            res = tempNode;
        }


can be rewritten as

 carry=data/10;
 Node tempNode = new Node(data%10);
 tempNode.setNext(res);
 res = tempNode;



findSum(Node l1, Node l2, int diff) 

you call int length1 = findLength(l1);int length2 = findLength(l2); in every recursive call. It's enough to call it once, before you enter the recursion. The result won't change in the middle. it'll reduce the complexity of your solution from O(N^2) to O(n)
All you need to do afterwards is decrement the value of diff until it becomes 0


I would rewrite the whole method as follows:

private static Node findSum(Node longer, Node shorter, int diff){
    if(diff==0){
         Node res = findSumForListOfSameSize(longer, shorter);
         return res;
    }
        Node res = findSum(longer.getNext(), shorter, diff--);
        int data = longer.getData() + carry;

        carry=data/10;
        Node tempNode = new Node(data%10);
        tempNode.setNext(res);
        res = tempNode;

        return res;
}


and the main would become

...
int length1 = findLength(l1);
int length2 = findLength(l2);
int diff = length1-length2;
if(diff&lt;0){
    diff = diff * -1;
    Node res = findSum(templ21, templ11, diff);
}else{
    Node res = findSum(templ11, templ21, diff);
}
...


you could remove the need for static variable carry by doing this:

Node res = findSum(longer.getNext(), shorter, diff--);
int prevSum = res.getData()
int carry = prevSum / 1;
res.setData(prevSum % 10)

int sum = longer.getData() + carry;
Node tempNode = new Node(sum);


you're 'delegating' the calculation of the carry (and update of the vaue) to the next digit calculation). Also note, I renamed the data variable to be prevSum and sum