Merge 2 sorted Linked List in Java

import java.util.Collections;
import java.util.LinkedList;
import java.util.TreeSet;

/**
*
*/

/**
* @author asharda
*
*/
public class merge2ll {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
merge();

}

public static void merge()
{
LinkedList<Integer>list1=new LinkedList<Integer>();
list1.add(10);
list1.add(8);
list1.add(13);
list1.add(11);
list1.add(16);
Collections.sort(list1);
LinkedList<Integer>list2=new LinkedList<Integer>();
list2.add(30);
list2.add(28);
list2.add(23);
list2.add(21);
list2.add(26);
Collections.sort(list2);
TreeSet <Integer>ts=new TreeSet<Integer>();
for(int i=0;i<list1.size();i++)
{
ts.add(list1.get(i));
}

for(int i=0;i<list2.size();i++)
{
ts.add(list2.get(i));
}

System.out.println(“Merged LL is”);
for(int t:ts)
{
System.out.println(t);
}
}

}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s