问题描述
seekhelp!howcanIcreate“publicstaticSortedListmergeList(SortedListlist1,SortedListlist2)”methodThismethodcanmergetwosortedlistsintoanewsortedone,andreturnareferenceofthenewlist.Theprogramshouldgothrougheachlinkiteminbothlists,anddirectlylinkthemintothenewlistinorder.Thanksforyourhelp!!nowIhaveclassLink{publiclongdData;//dataitempublicLinknext;//nextlinkinlist//————————————————————-publicLink(longdd)//constructor{dData=dd;}//————————————————————-publicvoiddisplayLink()//displaythislink{System.out.print(dData+”“);}}//endclassLink////////////////////////////////////////////////////////////////classSortedList{publicLinkfirst;//reftofirstitem//————————————————————-publicSortedList()//constructor{first=null;}//————————————————————-publicbooleanisEmpty()//trueifnolinks{return(first==null);}//————————————————————-publicvoidinsert(longkey)//insert,inorder{LinknewLink=newLink(key);//makenewlinkLinkprevious=null;//startatfirstLinkcurrent=first;//untilendoflist,while(current!=null&&key>current.dData){//orkey>current,previous=current;current=current.next;//gotonextitem}if(previous==null)//atbeginningoflistfirst=newLink;//first–>newLinkelse//notatbeginningprevious.next=newLink;//oldprev–>newLinknewLink.next=current;//newLink–>oldcurrnt}//endinsert()//————————————————————-publicLinkremove()//return&deletefirstlink{//(assumesnon-emptylist)Linktemp=first;//savefirstfirst=first.next;//deletefirstreturntemp;//returnvalue}//————————————————————-publicvoiddisplayList(){System.out.print(“List(first–>last):“);Linkcurrent=first;//startatbeginningoflistwhile(current!=null)//untilendoflist,{current.displayLink();//printdatacurrent=current.next;//movetonextlink}System.out.println(“”);}//publicstaticSortedListmergeList(SortedListlist1,SortedListlist2){//returna;//}}//endclassSortedList////////////////////////////////////////////////////////////////classSortedListApp{publicstaticvoidmain(String[]args){//createnewlistSortedListtheSortedList1=newSortedList();SortedListtheSortedList2=newSortedList();theSortedList1.insert(20);//insert2itemstheSortedList1.insert(40);theSortedList1.insert(10);//insert3moreitemstheSortedList1.insert(30);theSortedList1.insert(50);theSortedList1.displayList();//displaylisttheSortedList2.insert(25);//insert2itemstheSortedList2.insert(45);theSortedList2.insert(15);//insert3moreitemstheSortedList2.insert(35);theSortedList2.insert(55);theSortedList2.displayList();//displaylist}//endmain()}//endclassSortedListApp