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

一. 题目

输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增排序的.

代码请到我的代码库中下载 Point2Offer

二. 代码

package com;

/**
 * 剑指offer: 合并两个排序的链表
 * 方法:1.使用两个指针,递归实现;
 * 测试用例:功能测试(两个链表都有多个节点,节点的值互不相等或者存在值相等的多个节点)
 * 特殊输入测试(一个或两个链表为空,一个链表只有一个节点)
 * @author dingding
 * Date:2017-6-15 10:00
 * Declaration: All Rights Reserved!
 */
public class MergeSortedList {

    public static void main(String[] args) {
        test1();
        test2();
        test3();
    }

    //处理两个排序的链表
    private static ListNode merge(ListNode pHead1,ListNode pHead2){
        if (pHead1==null) {
            return pHead2;
        }
        if (pHead2==null) {
            return pHead1;
        }
        ListNode pMergeHead = null;
        if (pHead1.getValue()<pHead2.getValue()) {
            pMergeHead = pHead1;
            pMergeHead.setNext(merge(pHead1.getNext(), pHead2));    //递归(循环的替换)
        }else {
            pMergeHead = pHead2;
            pMergeHead.setNext(merge(pHead1, pHead2.getNext()));
        }
        return pMergeHead;
    }

    /*==================测试用例======================*/
    private static void test(ListNode pHead1,ListNode pHead2){
        System.out.println("输出合并后的链表: ");
        ListNode pMergeHead = merge(pHead1, pHead2);
        if (pMergeHead==null) {
            System.out.println("链表1和2都为空链表!");
        }else{
            while (pMergeHead!=null){
                System.out.print(pMergeHead.getValue()+" ");
                pMergeHead = pMergeHead.getNext();
            }
            System.out.println();
        }
    }

    //含有多个不同节点
    private static void test1() {
        ListNode first1 = new ListNode(1);
        ListNode second1 = new ListNode(3);
        ListNode third1 = new ListNode(5);
        ListNode fourth1 = new ListNode(7);
        ListNode fifth1 = new ListNode(9);

        first1.setNext(second1);
        second1.setNext(third1);
        third1.setNext(fourth1);
        fourth1.setNext(fifth1);

        ListNode first2 = new ListNode(2);
        ListNode second2 = new ListNode(4);
        ListNode third2 = new ListNode(5);
        ListNode fourth2 = new ListNode(8);
        ListNode fifth2 = new ListNode(10);

        first2.setNext(second2);
        second2.setNext(third2);
        third2.setNext(fourth2);
        fourth2.setNext(fifth2);

        test(first1, first2);
        System.out.println("==================================");
    }

    //有一个链表为空
    private static void test2() {
        ListNode first1 = new ListNode(1);
        ListNode second1 = new ListNode(3);
        ListNode third1 = new ListNode(4);
        ListNode fourth1 = new ListNode(7);
        ListNode fifth1 = new ListNode(9);

        first1.setNext(second1);
        second1.setNext(third1);
        third1.setNext(fourth1);
        fourth1.setNext(fifth1);

        test(first1, null);
        System.out.println("==================================");
    }

    //两个链表都为空
    private static void test3(){
        test(null, null);
    }

}

链表类

package com;
/**
 * 链表类
 * @author dingding
 *
 */

public class ListNode {
    public int value;
    public ListNode next;

    public ListNode(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public ListNode getNext() {
        return next;
    }
    public void setNext(ListNode next) {
        this.next = next;
    }
}

有不妥当之处,麻烦告知:D

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值