# Java, 1 ms, 4 lines codes, using recursion

• ``````public ListNode mergeTwoLists(ListNode l1, ListNode l2){
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val < l2.val){
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else{
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}``````

• @yangliguang Hi I am new here, can you explain why in line 6 you return l1? Thank you!

• Amazing! Solution

• Brilliant!!!!!

• @bijizhu because that is the new head, with smaller value.

• How can this be just 1ms?
I run it on my computer,it took about 14ms,why?

• Thx! I only think iterative version.

• @Skyfacon you are right it doesn't take 1ms.When I coped and submitted this code I got 15 ms.

• @yangliguang Please let us know how you got 1 ms for this solution.Copy pasting and submitting your code gives 15 ms to me.

• @nikhilaroratgo The server of leetcode is quite unstable so u could expect a different run-time for the same algorithm at different time of the day

• good!!!!!!!!!!!!

• I am new here. I am just curious about if the order will affect the code. For example, L1 is descending ordered while L2 is ascending ordered. Thanks. It doesn't state this very clearly.

• @jober Sometimes, I have the same problem with you. I solve the problem by analysing the test cases.

• This code can run `1ms`? Unbelievable!!!

• @bijizhu
it's because l1 is the head of the return List ( with smallest value of the entire list )

• 1ms？不存在的。 good solution

• 1ms? not 1ms!
4 lines? not 4 lines!

But I still vote up. Nice one.

• @Skyfacon hhhh i used 18ms

• This post is deleted!

• I like this solution, very good!

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