P114、面试题17:合并两个排序的链表

题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增顺序的。
struct ListNode{
      int    m_nKey;
      ListNode*    m_pNext;
}

测试用例:
1)功能测试(输入的两个链表有多个结点,结点的值互不相同或者存在值相等的多个结点);
2)特殊输入测试(两个链表的一个或者两个头结点为null指针、两个链表中只有一个结点)。
 
实现代码:
package com.yyq;

/**
 * Created by Administrator on 2015/9/15.
 */
public class Merge {
    public static ListNode merge(ListNode pHead1, ListNode pHead2){
        if (pHead1 == null)
            return pHead2;
        else if(pHead2 == null)
            return pHead1;
        ListNode pMergedHead = null;
        if (pHead1.getM_nValue() < pHead2.getM_nValue()){
            pMergedHead = pHead1;
            pMergedHead.setM_pNext(merge(pHead1.getM_pNext(), pHead2));
        }
        else{
            pMergedHead = pHead2;
            pMergedHead.setM_pNext(merge(pHead1,pHead2.getM_pNext()));
        }
        return pMergedHead;
    }

    public static void printList(ListNode pListHead) {
        if (pListHead == null)
            return;
        ListNode pNode = pListHead;
        while (pNode != null) {
            System.out.print(pNode.getM_nValue() + "  ");
            pNode = pNode.getM_pNext();
        }
        System.out.println();
    }

    // ====================测试代码====================
    public static ListNode Test(String testName, ListNode pHead1, ListNode pHead2)
    {
        if(testName != null)
            System.out.println(testName+" begins:");
        System.out.println("The first list is:");
        printList(pHead1);
        System.out.println("The second list is:");
        printList(pHead2);

        System.out.println("The merged list is:");
        ListNode pMergedHead = merge(pHead1, pHead2);
        printList(pMergedHead);
        return pMergedHead;
    }

    // list1: 1->3->5
    // list2: 2->4->6
    public static void Test1()
    {
        ListNode pNode1 = new ListNode(1);
        ListNode pNode3 = new ListNode(3);
        ListNode pNode5 = new ListNode(5);
        pNode1.setM_pNext(pNode3);
        pNode3.setM_pNext(pNode5);

        ListNode pNode2 = new ListNode(2);
        ListNode pNode4 = new ListNode(4);
        ListNode pNode6 = new ListNode(6);
        pNode2.setM_pNext(pNode4);
        pNode4.setM_pNext(pNode6);

        Test("Test1", pNode1, pNode2);
        pNode1 = null;
        pNode2 = null;
    }

    // 两个链表中有重复的数字
    // list1: 1->3->5
    // list2: 1->3->5
    public static void Test2()
    {
        ListNode pNode1 = new ListNode(1);
        ListNode pNode3 = new ListNode(3);
        ListNode pNode5 = new ListNode(5);
        pNode1.setM_pNext(pNode3);
        pNode3.setM_pNext(pNode5);

        ListNode pNode2 = new ListNode(1);
        ListNode pNode4 = new ListNode(3);
        ListNode pNode6 = new ListNode(5);
        pNode2.setM_pNext(pNode4);
        pNode4.setM_pNext(pNode6);

        Test("Test2", pNode1, pNode2);
        pNode1 = null;
        pNode2 = null;
    }

    // 两个链表都只有一个数字
    // list1: 1
    // list2: 2
    public static void Test3()
    {
        ListNode pNode1 = new ListNode(1);
        ListNode pNode2 = new ListNode(2);

        Test("Test3", pNode1, pNode2);
        pNode1 = null;
        pNode2 = null;
    }

    // 一个链表为空链表
// list1: 1->3->5
// list2: 空链表
    public static void Test4()
    {
        ListNode pNode1 = new ListNode(1);
        ListNode pNode3 = new ListNode(3);
        ListNode pNode5 = new ListNode(5);
        pNode1.setM_pNext(pNode3);
        pNode3.setM_pNext(pNode5);
        Test("Test4", pNode1, null);
        pNode1 = null;
    }

    // 两个链表都为空链表
    // list1: 空链表
    // list2: 空链表
    public static void Test5()
    {
        Test("Test5", null, null);
    }

    public static void main(String[] args)
    {
        Test1();
        Test2();
        Test3();
        Test4();
        Test5();
    }
}
 
输出结果:
Test1 begins:
The first list is:
1  3  5  
The second list is:
2  4  6  
The merged list is:
1  2  3  4  5  6  
Test2 begins:
The first list is:
1  3  5  
The second list is:
1  3  5  
The merged list is:
1  1  3  3  5  5  
Test3 begins:
The first list is:
1  
The second list is:
2  
The merged list is:
1  2  
Test4 begins:
The first list is:
1  3  5  
The second list is:
The merged list is:
1  3  5  
Test5 begins:
The first list is:
The second list is:
The merged list is:
posted @ 2015-11-03 15:08  我是一名老菜鸟  阅读(240)  评论(0编辑  收藏  举报