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; } |
She need to be betrayed to know how naive she isðŸ‘ï¸ðŸ‘„ðŸ‘ï¸
Hi. I know about reference types but I don’t understand how `head` get its value. what topics should I read about to know how its works?
Made a video for explaining a JavaScript solution:
Chinese: https://www.youtube.com/watch?v=6ymPLmf55PM
English: https://www.youtube.com/watch?v=RhgFmrFgZTo
Facebook: https://www.facebook.com/groups/2094071194216385/
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
lol we don’t have to move all pointers, its alright to just copy its reference
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;
}
Thanks, much simple solution.
using recursion space complexity is O(n).
space complexity O(1) would be much better.
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
What ÐлекÑандр Ефремов says is correct. Pointers must be moved
for me recursion worked :-
public static Node mergeLists(Node headA, Node headB) {
Node headC = null;
if (headA == null) {
return headB;
}
if (headB == null) {
return headA;
}
if (headA.data <= headB.data) {
headC = headA;
headC.next = mergeLists(headA.next, headB);
} else {
headC = headB;
headC.next = mergeLists(headA, headB.next);
}
return headC;
}
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; }
Did it with recursion here
http://www.algorithmsandme.com/2013/10/linked-list-merge-two-sorted-linked.html#.VQkTTfmUfA0
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;
}
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;
}
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;
}
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;
i will set fakeHead = null in the end to prevent leak