File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .List ;
2+ import java .util .LinkedList ;
3+
4+
5+ public class MergeTwoSortedLists {
6+ public static void main (String [] args ) {
7+ List <Integer > list1 = new LinkedList <>();
8+
9+ List <Integer > list2 = new LinkedList <>();
10+
11+ List <Integer > resultList = new LinkedList <>();
12+
13+ list1 .add (1 );
14+ list1 .add (3 );
15+ list1 .add (6 );
16+
17+ list2 .add (2 );
18+ list2 .add (4 );
19+ list2 .add (5 );
20+
21+ int i = 0 ;
22+ int j = 0 ;
23+
24+ while (i < list1 .size () && j < list2 .size ()){
25+ if (list1 .get (i ) < list2 .get (j )){
26+ resultList .add (list1 .get (i ));
27+ i ++;
28+ }
29+
30+ else {
31+ resultList .add (list2 .get (j ));
32+ j ++;
33+ }
34+ }
35+
36+ while (i < list1 .size ()){
37+ resultList .add (list1 .get (i ));
38+ i ++;
39+ }
40+
41+ while (j < list2 .size ()){
42+ resultList .add (list2 .get (j ));
43+ j ++;
44+ }
45+
46+ System .out .print ("Resulting List After the Merging the Two Sorted Lists : " );
47+
48+ for (int k = 0 ;k <resultList .size ();k ++){
49+ System .out .print (resultList .get (k ) + " " );
50+ }
51+
52+ System .out .println ("" );
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments