This question was asked in GOOGLE's coding round.
Question:
Write an algorithm to merge two sorted linked lists.
solution:
package com.shashi;
import java.util.LinkedList;
import java.util.List;
public class MergeSortedList {
public static void main(String []args)
{
List<Integer>list1=new LinkedList<Integer>();
list1.add(10);
list1.add(30);list1.add(60);list1.add(90);
list1.add(790);
List<Integer>list2=new LinkedList<Integer>();
list2.add(10);
list2.add(30);
list2.add(89);
list2.add(89);
list2.add(232);
list2.add(666);
mergeLists(list1,list2);
}
public static void mergeLists(List<Integer>l1,List<Integer>l2)
{
if(l1.size()==0 && l2.size()==0)
{
System.out.print("Emppty lists were given");
}
if(l1.size()==0)
{
System.out.print(l2);
return;
}
if(l2.size()==0)
{
System.out.print(l1);
return;
}
//now actual algorithm starts!!!!!!!
int index1=0,index2=0;
List<Integer> fill=new LinkedList<Integer>();
for(;index1<l1.size() && index2<l2.size();)
{
if(l1.get(index1)<l2.get(index2))
{
fill.add(l1.get(index1++));
if(index1==l1.size())
{
while(index2<l2.size())
{
fill.add(l2.get(index2++));
}
break;
}
}
else
{
fill.add(l2.get(index2++));
if(index2==l2.size())
{
while(index1<l1.size())
{
fill.add(l1.get(index1++));
}
break;
}
}
}
//print merged data
System.out.print(fill);
}
}
out put:
---------------------------
[10, 10, 30, 30, 60, 89, 89, 90, 232, 666, 790]
--------------------------------
explanation:
--- first i created two sorted linkd lists[//be sure they are in order].
---then i passed the linked lists to mergeLists() method ,which was defined outside class.
---pre conditions:
if(list1 is null)
then list2 itself merged list
if(list2 is null)
then list1 itself merged list
if(list1 and list2 is null)
then invalid list or size of both lists were empty.
//if none of above case is true then apply logic defined as
MOVE:
if(an element in list1<an element is list2)
then fill new list with element of list1
else
fill new list with element of list2
repeat MOVE untill both lists were filled in new list.
Question:
Write an algorithm to merge two sorted linked lists.
solution:
package com.shashi;
import java.util.LinkedList;
import java.util.List;
public class MergeSortedList {
public static void main(String []args)
{
List<Integer>list1=new LinkedList<Integer>();
list1.add(10);
list1.add(30);list1.add(60);list1.add(90);
list1.add(790);
List<Integer>list2=new LinkedList<Integer>();
list2.add(10);
list2.add(30);
list2.add(89);
list2.add(89);
list2.add(232);
list2.add(666);
mergeLists(list1,list2);
}
public static void mergeLists(List<Integer>l1,List<Integer>l2)
{
if(l1.size()==0 && l2.size()==0)
{
System.out.print("Emppty lists were given");
}
if(l1.size()==0)
{
System.out.print(l2);
return;
}
if(l2.size()==0)
{
System.out.print(l1);
return;
}
//now actual algorithm starts!!!!!!!
int index1=0,index2=0;
List<Integer> fill=new LinkedList<Integer>();
for(;index1<l1.size() && index2<l2.size();)
{
if(l1.get(index1)<l2.get(index2))
{
fill.add(l1.get(index1++));
if(index1==l1.size())
{
while(index2<l2.size())
{
fill.add(l2.get(index2++));
}
break;
}
}
else
{
fill.add(l2.get(index2++));
if(index2==l2.size())
{
while(index1<l1.size())
{
fill.add(l1.get(index1++));
}
break;
}
}
}
//print merged data
System.out.print(fill);
}
}
out put:
---------------------------
[10, 10, 30, 30, 60, 89, 89, 90, 232, 666, 790]
--------------------------------
explanation:
--- first i created two sorted linkd lists[//be sure they are in order].
---then i passed the linked lists to mergeLists() method ,which was defined outside class.
---pre conditions:
if(list1 is null)
then list2 itself merged list
if(list2 is null)
then list1 itself merged list
if(list1 and list2 is null)
then invalid list or size of both lists were empty.
//if none of above case is true then apply logic defined as
MOVE:
if(an element in list1<an element is list2)
then fill new list with element of list1
else
fill new list with element of list2
repeat MOVE untill both lists were filled in new list.
No comments:
Post a Comment