题目及用例
package pid021;
/*合并两个有序链表
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
*/
import pid021.LinkList;
import pid021.LinkList.ListNode;
public class main {
public static void main(String[] args) {
LinkList a=new LinkList(1);
a.addLast(2);
a.addLast(5);
LinkList b=new LinkList(-5);
b.addLast(0);
b.addLast(500);
a.printList();
b.printList();
test(a.first,b.first);
LinkList c=new LinkList(1);
c.addLast(2);
c.addLast(500);
LinkList d=new LinkList(1);
d.addLast(2);
d.addLast(3);
c.printList();
d.printList();
test(c.first,d.first);
}
private static void test(ListNode ito,ListNode ito2) {
Solution solution = new Solution();
ListNode rtn;
long begin = System.currentTimeMillis();
System.out.println();
//开始时打印数组
rtn=solution.mergeTwoLists(ito,ito2);//执行程序
long end = System.currentTimeMillis();
rtn.printNodeToEnd();
//System.out.println(":rtn" );
//System.out.print(rtn);
System.out.println();
System.out.println("耗时:" + (end - begin) + "ms");
System.out.println("-------------------");
}
}
linklist
package pid021;
public class LinkList {
public class ListNode {
public int val;
public ListNode next;
ListNode(int x)
{ val = x;
next=null;
}
public ListNode() {
val=0;
next=null;
}
public void printNodeToEnd(){
ListNode a=this;
System.out.println("print node to end");
while(a!=null){//直到最后一个不为空的节点
System.out.print(a.val+" ");
a=a.next;
}
System.out.println();
}
}
ListNode first;
public LinkList(int x){
first=new ListNode(x);
first.next=null;
}
public LinkList(){
first=null;
first.next=null;
}
public void addLast(int x){
if(first==null){
first=new ListNode(x);
return;
}
ListNode a=first;
while(a.next!=null){
a=a.next;
}
ListNode b=new ListNode(x);
a.next=b;
}
public void printList(){
ListNode a=first;
System.out.println("print listnode");
while(a!=null){//直到最后一个不为空的节点
System.out.print(a.val+" ");
a=a.next;
}
System.out.println();
}
}
解法1(成功,速度很慢,25ms)
逐次比较两个链表的头,小的变为结果集的尾部,两者指针皆向前移动一步
package pid021;
import pid021.LinkList;
import pid021.LinkList.*;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode a=l1;
ListNode b=l2;
ListNode head=null;
ListNode now=null;
if(a==null){
return l2;
}
if(b==null){
return l1;
}//异常情况
if(a.val<=b.val){
head=a;
now=a;
a=a.next;
}
else{
head=b;
now=b;
b=b.next;
}//初始化
while(a!=null&&b!=null){
if(a.val<=b.val){//谁比较小,则让结果链的下一个为他,结果链和这个链各向后一步
now.next=a;
a=a.next;
now=now.next;
}
else{
now.next=b;
b=b.next;
now=now.next;
}
}
if(a==null){
now.next=b;
}
if(b==null){
now.next=a;
}
return head;
}
}
解法2(成功,2ms,很快)
思路与上面一样,不过不用设置ab,直接用l1,l2作为指针即可
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
//排除空的情况
if(l1==null){
return l2;
}
if(l2==null){
return l1;
}
//设置结果的头结点
ListNode head;
if(l2.val<l1.val){
head=l2;
l2=l2.next;
}
else{
head=l1;
l1=l1.next;
}
ListNode now=head;
//不断设置now的next(比较l1和l2),直到有一个为null
while(l1!=null&&l2!=null){
if(l2.val<l1.val){
now.next=l2;
l2=l2.next;
}
else{
now.next=l1;
l1=l1.next;
}
now=now.next;
}
//将剩余节点放入now的next
if(l1==null){
now.next=l2;
return head;
}
else{
now.next=l1;
return head;
}
}
解法3(别人的)
递归地决定下一个添加到结果里的值。如果两个链表都是空的,那么过程终止,所以递归过程最终一定会终止。
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
else if (l2 == null) {
return l1;
}
else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
}
else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}