《剑指offer》合并两个排序的链表

题目

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

思路1 递归

这题要用递归,得先把画图把规律找出来。如果表1当前值小于表2当前值,表1当前值成为新链表的表头,否则返回表2的当前值作为新链表的表头。
例子:
表1 1>2>6>8
表2 5>8
当表1当前值 1< 表2当前值 5
表1的值 1 变成新链表的表头,这时需要比较 1的next值和 表2的当前值 5,也就是判断 1 拿出去后,剩下两个链表的合并情况。
返回 表1的值(新链表的头)

进入递归过程,比较 表1的next值 2 和 表2的当前值 5,有2<5
2 变成新链表的表头,和上一个节点1接起来。这时需要比较 2的next值 6和 表2的当前值 5,也就是判断 2 拿出去后,剩下两个链表的合并情况。
返回 2(子递归里面 新链表的头)

进入递归过程,比较 6 和 表2的当前值 5,有6>5
5 变成新链表的表头,和上一个节点2接起来。这时需要比较 5的next值 8和 表1的当前值 6,也就是判断 5拿出去后,剩下两个链表的合并情况。
返回 5(子递归里面 新链表的头)

进入递归过程,比较 8 和 表1的当前值6,有8>6
6 变成新链表的表头,和上一个节点5接起来。这时需要比较 6的next值 8和 表1的当前值 8,也就是判断 6拿出去后,剩下两个链表的合并情况。
返回 6(子递归里面 新链表的头)

进入递归过程,比较 8 和 表1的当前值8,有8>=8
8 变成新链表的表头,和上一个节点6接起来。这时需要比较 8的next值 null 和 表2的当前值 8,也就是判断 8拿出去后,剩下两个链表的合并情况。
返回 8(子递归里面 新链表的头)

进入递归过程,比较 null 和 表2的当前值8,表1null则返回表2值 8
8 变成新链表的表头,和上一个节点8接起来。
返回 8(子递归里面 新链表的头)

从最底层的8开始算, 1>2>5>6>8>8

这里写图片描述

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
       if(list1==null)
           return list2;
        if(list2==null)
            return list1;
        ListNode res=null;
        if(list1.val<list2.val)
        {
            res=list1;
            list1.next=Merge(list1.next,list2);
        }
         else
         {   
             res=list2;
             list2.next=Merge(list1,list2.next);
         }
        return res;
    }
}

思路2 非递归

建一个新链表newnode,其中newnode是头结点,curnode是尾结点。初始化2结点都是空,然后newnode指向尾结点curnode
比较list1和list2的当前值,谁比较小,谁的当前值就加入新链表(尾结点curnode下一个结点指向该值,然后curnode往后挪动成为新的尾结点),接着该表的当前值往后走一位,循环以上操作。
如果2个表中有个表挪到尾部了(null),就把剩下还有的那个表全部加到新链表里。

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
       if(list1==null)
           return list2;
        if(list2==null)
            return list1;
        ListNode curnode=null;
        ListNode newnode=null;
        while(list1!=null&&list2!=null)
        {
            if(list1.val<list2.val)
            {
                if(newnode==null)
                {
                    curnode=list1;
                    newnode=curnode;
                }
                else
                {
                    curnode.next=list1;
                    curnode=curnode.next;
                }
                list1=list1.next;

            }
            else
            {  
                if(newnode==null)
                {
                    curnode=list2;
                    newnode=curnode;
                }
                else
                {
                    curnode.next=list2;
                    curnode=curnode.next;
                }
                list2=list2.next;
            }
        }
        if(list1==null)
        {
           curnode.next=list2;

        }
        else
        {
            curnode.next=list1;
        }       
        return newnode;
    }
}

猜你喜欢

转载自blog.csdn.net/gsch_12/article/details/81156451