Java"merge two sorted lists into a new sorted one"

问题描述

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

解决方案

时间: 2024-11-01 18:02:58

Java"merge two sorted lists into a new sorted one"的相关文章

LeetCode 23 Merge k Sorted Lists(合并K个已排序链表)

翻译 合并K个已排序的链表,并且将其排序并返回. 分析和描述其复杂性. 原文 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 代码 我们采用分治的方法来解决这个问题,其有K个链表,不断将其划分(partition),再将其归并(merge). 划分的部分并不难,将其不断分成两部分,但是需要注意的是可能出现start和end相等的情况,这时候就直接r

[LeetCode]21.Merge Two Sorted Lists

[题目] 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. [分析] 无 [代码] /********************************* * 日期:2015-01-06 * 作者:SJF0115 * 题目: 21.Merge Two Sorted L

[LeetCode]23.Merge k Sorted Lists

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [分析] 无 [代码] /********************************* * 日期:2015-01-06 * 作者:SJF0115 * 题目: 23.Merge k Sorted Lists * 来源:https://oj.leetcode.com/problems/me

LeetCode 21 Merge Two Sorted Lists(合并两个已排序的数组)

翻译 合并两个排好序的链表,并返回这个新链表. 新链表应该由这两个链表的头部拼接而成. 原文 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. 代码 /** * Definition for singly-linked list. * struct ListNode

Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:开始做过两两合并的链表,此时做k路合并,即将k个链表进行合并,可以先将这k个链表进行两两合并,知道合并为一个链表.直接调用前面写过的两两合并的例子.   C++代码实现: #include<iostream> #include<new> #include<vector>

Merge Two Sorted Lists

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. C++代码如下: #include<iostream> #include<new> using namespace std; //Definition for singly-linked list. s

java.text.format 将字符串“060503”转化为06:05:03或者将&amp;quot;20081002102030“转化为2008-10-02 10:00:30

直接贴代码,不解释: package com.chapter.five.one.tcn; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDate2 { public static void main(String[] args) throws ParseException

java 如何将string=&amp;amp;quot;Sun Jan 02 2011 00:00:00 GMT+0800&amp;amp;quot; 转换日期啊,

问题描述 String strdate=this.getRequest().getParameter("vehicleInformTime")System.out.println(strdate);vehicleInformTime = DateFormate.getDateByStr(strdate);/** * 将String转换为Date * @param str 如果日期str 为原始格式, dateFormat不用指定其格式 * @return */public static

java程序编译成功但运行不了(Exception in thread &amp;amp;quot;main&amp;amp;quot; java.lang.IllegalArgumentException

问题描述 D:work>javac JComboBoxDemo.javaD:work>java JComboBoxDemoException in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself at java.awt.Container.checkAddToSelf(Unknown Source) at java.awt.Container.addI