The Java solution with 0 milli second

• My Solution :

`````` public class Solution {
boolean flag = false;
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1.val = l1.val + l2.val + (flag ? 1 : 0);
if (l1.val >= 10) {
l1.val = l1.val % 10;
flag = true;
} else {
flag = false;
}

if(l1.next != null) {
l1.next = l1.next!=null?l1.next:new ListNode(0);
l2.next = l2.next!=null?l2.next:new ListNode(0);
}
if(l1.next==null && flag)
{
l1.next=new ListNode(1);
}
return l1;
}
}
``````

My Test :

``````    public class ListNode {

public int val;
public ListNode next;

public ListNode(int x) {
val = x;
}
}

public class LeetCode {

public static void main(String[] args) {
System.out.println(">> START TIME : "+ System.currentTimeMillis());
ListNode listNode1 = new ListNode(1);

ListNode listNode4 = new ListNode(7);
ListNode listNode5 = new ListNode(3);

listNode4.next = listNode5;

Solution atn =  new Solution();

while (true){
if(listNode1!=null){
System.out.println("List VAL : "+listNode1.val);
listNode1 = listNode1.next;
}
else
{
break;
}

}
System.out.println(">> END TIEM : "+System.currentTimeMillis());

}

}

For me if Input : [1], [7,3]

Result is : [8,3]

**Check below the copy from IDE**

run:
>> START TIME : 1438665003506
List VAL : 8
List VAL : 3
>> END TIEM : 1438665003506
BUILD SUCCESSFUL (total time: 0 seconds)``````

• That doesn't even compile, as your test tries to use class `AddTwoNumbers`, which doesn't exist. Fixing it to `Solution` results in just the 7 being printed, and there's no way it prints 7 and 3 for you. Btw, you shouldn't ignore the result of the `addTwoNumbers` method.

• Sorry My Mistake. Update the main class with changing the logical class name ..

Try Now :

public class LeetCode {

``````/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println(">> START TIME : "+ System.currentTimeMillis());
ListNode listNode1 = new ListNode(1);

ListNode listNode4 = new ListNode(7);
ListNode listNode5 = new ListNode(3);

listNode4.next = listNode5;

Solution atn =  new Solution();

while (true){
if(listNode1!=null){
System.out.println("List VAL : "+listNode1.val);
listNode1 = listNode1.next;
}
else
{
break;
}

}
System.out.println(">> END TIEM : "+System.currentTimeMillis());

}
``````

Why its printing both the Value for me in the IDE then. Where m I ignoring the result of addTwoNumbers method.

• This post is deleted!

• Why its printing both the Value for me in the IDE then.

Don't know. Maybe recompile everything, make sure it's not using obsolete code?

Where m I ignoring the result of addTwoNumbers method

At both places where you call it. For the recursive call maybe it makes sense, but the outside caller should use it. Not the cause of your problem, of course, but also not clean.

• I found the issue ..
I missed a or condition .. resolved

public class Solution {
boolean flag = false;
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
l1.val = l1.val + l2.val + (flag ? 1 : 0);
if (l1.val >= 10) {
l1.val = l1.val % 10;
flag = true;
} else {
flag = false;
}

``````    if(l1.next != null || l2.next != null) {
l1.next = l1.next!=null?l1.next:new ListNode(0);
l2.next = l2.next!=null?l2.next:new ListNode(0);
}
if(l1.next==null && flag)
{
l1.next=new ListNode(1);
}
return l1;
}
``````

}

• Did you find out why it "worked" for you locally?

And what's up with that new title? The solution in the question still doesn't work, and the one in your comment above takes several hundred milliseconds, like everybody else's.

• you are suppose the size of l1 is no smaller than l2?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.