LeetCode Merge Two Sorted Lists && 基础算法-合并有序数组

Quest: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.

合并两个有序数列

题目给出的原型类

    public  class ListNode {
              int val;
              ListNode next;
              ListNode(int x) { val = x; }
    }
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    }

需要考虑的情况

1、  l1  is null

2、 l2   is null

3、 both are not null

该题的解法最关键的就是新建一个链头,然后再这个链头的后面添加元素,最后返回链头.next。

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        //将l2合并到l1
        ListNode newListNode=new ListNode(Integer.MIN_VALUE);
        ListNode tempListNode=newListNode;
        ListNode index1=l1;
        ListNode index2=l2;
        while(index2!=null)
        {
            if(index1!=null)//l1已经没有了
            {
                if(index1.val>index2.val)
                {
                    tempListNode.next=index2;
                    tempListNode=tempListNode.next;
                    index2=index2.next;
                }
                else
                {
                    tempListNode.next=index1;
                    tempListNode=tempListNode.next;
                    index1=index1.next;
                }
            }
            else//剩下的都是l2
            {
                tempListNode.next=index2;
                break;
            }
        }
        // if the length of l1 >length of l2
        if(index1!=null)
        {
            tempListNode.next=index1;
        }
        return newListNode.next;
    }

测试代码

    public static void main(String[] args) throws Exception  {
        Main main=new Main();
        ListNode l1=main.new ListNode(1);
        ListNode l2=main.new ListNode(5);
        ListNode l3=main.new ListNode(10);
        ListNode l4=main.new ListNode(15);
        ListNode l5=main.new ListNode(20);
        l1.next=l2;
        l2.next=l3;
        l3.next=l4;
        l4.next=l5;
        
        ListNode l6=main.new ListNode(2);
        ListNode l7=main.new ListNode(10);
        ListNode l8=main.new ListNode(11);
        ListNode l9=main.new ListNode(13);
        ListNode l10=main.new ListNode(18);
        l6.next=l7;
        l7.next=l8;
        l8.next=l9;
        l9.next=l10;
        
        ListNode l=main.mergeTwoLists(l1, l6);
        while(true)
        {
            if(l!=null)
            {
                System.out.println(l.val);
                l=l.next;
            }
            else
            {
                break;
            }
        }
     } 

输出

1
2
5
10
10
11
13
15
18
20

 整个思路和合并数组类似,不同的是合并数组知道长度。

先new一个准备返回的ListNode对象,然后向这个ListNode后面添加。

先遍历其中之一比如l1,将l1的val和l2的val相比较,将小的放在newListNode后面,并更新index

假如l2有剩余,放在后面即可

 

 

 

2、合并有序数组

算法流程:

   while(A中还有未遍历元素&&B中也还有未遍历元素){

    如果A[i]<B[j]         

             C[i+j]= A[i];

             i++;

     否则

             C[i+j]=B[j];   

             j++;

   while(A中还有未遍历的元素)

            C[j+i++]=A[i++];

 

  while(B中还有未遍历的元素)

           C[i+j++]=B[j++];

 

Java 实现:

    public static int[] mergeTwoArrays(int [] i1,int [] i2)
    {
        int len1=i1.length;
        int len2=i2.length;
        int len=len1+len2;
        int [] n=new int[len];
        int index=0,index1=0,index2=0;
        while(index1<len1 && index2<len2)
        {
            if(i1[index1]<i2[index2])
            {
                n[index++]=i1[index1];
                index1++;
            }
            else
            {
                n[index++]=i2[index2];
                index2++;                
            }
        }
        while(index1<len1)
        {
            n[index++]=i1[index1];
            index1++;
        }
        while(index2<len2)
        {
            n[index++]=i2[index2];
            index2++;
        }
        return n;
    }

测试代码

        int[] i1={1,3,5,7,9,11,13,15,17};
        int[] i2={2,4,6,8,10,11};
        int[] n=mergeTwoArrays(i1, i2);
        System.out.println(Arrays.toString(n));

输出

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 13, 15, 17]

posted @ 2015-07-08 14:51  Maydow  阅读(201)  评论(0编辑  收藏  举报