# LeetCode – Merge Two Sorted Lists (Java)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Java Solution

The key to solve the problem is defining a fake head. Then compare the first elements from each list. Add the smaller one to the merged list. Finally, when one of them is empty, simply append it to the merged list, since it is already sorted.

```public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode head = new ListNode(0); ListNode p=head;   ListNode p1=l1; ListNode p2=l2; while(p1!=null && p2!=null){ if(p1.val < p2.val){ p.next = p1; p1 = p1.next; }else{ p.next = p2; p2 = p2.next; } p=p.next; }   if(p1!=null){ p.next = p1; }   if(p2!=null){ p.next = p2; }   return head.next; }```
Category >> Algorithms
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
```<pre><code>
String foo = "bar";
</code></pre>
```
• Cat Racket
• Md Saif

Why to use break; ?? because we can have more than one value in first linked list as compared to other. example L1= 1 ,2, 6 ,4 ,5. l2= 5,3,9

• Kartik Downey Jr.

lol we don’t have to move all pointers, its alright to just copy its reference

• Shelly Jaglan

code has mistake
correct code
in
else if(l1==null){
p.next = l2;
l2 = l2.next;
p = p.next;
break;
}
else if(l2==null){
p.next = l1;
l1 = l1.next;
p = p.next;
break;
}

• tunca tunç

Thanks, much simple solution.
using recursion space complexity is O(n).
space complexity O(1) would be much better.

• Vivek Muralidharan

I liked the core of your solution of creating a fakehead. But, unfortunately it is not complete. Below is the complete non-recursive solution for the above problem in JAVA:

``` public class MergingTwoSortedLists { private class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } ListNode head1; ListNode head2; public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode list1 = l1; ListNode list2 = l2; ListNode head = new ListNode(0); ListNode merged = head; if(l1 == null && l2 != null){ return l2; } if(l1 != null && l2 == null){ return l1; } while(list1 != null && list2 != null){ if(list1.val < list2.val){ merged.next = list1; list1 = list1.next; } else{ merged.next = list2; list2 = list2.next; } merged = merged.next; } //If list1 alone is present while(list1 != null){ merged.next = list1; list1 = list1.next; merged = merged.next; } //If list2 alone is present while(list2 != null){ merged.next = list2; list2 = list2.next; merged = merged.next; } return head.next; } public void display(ListNode head){ ListNode temp = head; while(temp != null){ System.out.print(temp.val+" "); temp = temp.next; } System.out.println(); } public static void main(String[] args){ MergingTwoSortedLists obj = new MergingTwoSortedLists(); obj.head1 = obj.new ListNode(1); obj.head1.next = obj.new ListNode(3); obj.head2 = obj.new ListNode(2); obj.head2.next = obj.new ListNode(4); obj.display(obj.head1); obj.display(obj.head2); obj.display(obj.mergeTwoLists(obj.head1,obj.head2)); ```

``` } } ```</pre

• Joe West

What Александр Ефремов says is correct. Pointers must be moved

• for me recursion worked :-

}
}

} else {
}
}

• Ankit Shah

the last two loops should be while loop instead of if loops, consider an example where all the elements in l1 are smaller then all the elements in l2, in which case last if loop has to be a while and you also need to move the p2 pointer (p2 = p2.next)

• Александр Ефремов

Code is wrong

if(p1 != null)
p.next = p1;
if(p2 != null)
p.next = p2;

You should move pointers:
if(p1 != null)
{ p.next = p1; p = p.next; p1=p1.next; }
if(p2 != null)
{ p.next = p2; p = p.next; p2=p2.next; }

• Salil Surendran

The above solution doesn’t complete. Here is my solution:

public static Node mergeTwoSortedLists(Node root1,Node root2){
if(root1 == null)
return root2 == null?null:root2;
if(root2 == null)
return root1;

Node returnNode, n1;
returnNode = n1 = root1.value < root2.value?root1:root2;
Node n2 = root1.value < root2.value?root2:root1;

while(n1 != null && n2 != null){
while(n1.child != null && n1.child.value <= n2.value)
n1 = n1.child;
Node oldChild = n1.child;
n1.child = n2;
n1 = n2;
n2 = oldChild;
}
return returnNode;
}

• mutepoet

Recursive solution:

``` public static ListNode merge(ListNode n1, ListNode n2) { if (n1 == null) return n2; if (n2 == null) return n1;```

``` ```

``` ListNode node; if (n1.val < n2.val) { node = n1; node.next = merge(n1.next, n2); } else { node = n2; node.next = merge(n1, n2.next); } return node; } ```

• Wally Osmond

I tried to do this with recursion and got stuck however I’m sure it’s possible. i also implemented this using two stacks which IMHO is a lot cleaner code than tracing loops.

public static ListNode getStacked (ListNode l1, ListNode l2) {

Stack s1= new Stack();

Stack s2= new Stack();

ListNode result=null,t=null;

while(l1!=null) {

s1.push(l1);

l1=l1.next;

}

while(l2!=null) {

s2.push(l2);

l2=l2.next;

}

while(!s1.isEmpty() && !s2.isEmpty()) {

if(s1.peek().val > s2.peek().val)

t=s1.pop();

else

t=s2.pop();

t.next=result;

result=t;

}

if(s1.isEmpty()) {

while(!s2.isEmpty()) {

t=s2.pop();

t.next=result;

result=t;

}

} else {

while(!s1.isEmpty()) {

t=s1.pop();

t.next=result;

result=t;

}

}

return result;

}

• Mufasa

After the while, considering that p = p1 or p = p2 is it necessary to do:
if(p1 != null)
p.next = p1;
if(p2 != null)
p.next = p2;

• junmin

i will set fakeHead = null in the end to prevent leak