java实现---合并两个有序链表,合并后依然有序

  • cur1 和cur2 分别是两条有序链表,result 则为合并之后的结果链表,tail为结果链表的最后一个节点,方便尾插
  • cur1 和 cur2 同时往后走,两个指针所指的数分别比较,拿出来的节点尾插在result链表中
    在这里插入图片描述
class ListNode{
    int data;
    ListNode next;
}

public class Link{
    public static ListNode MergeOrderedList(ListNode first,ListNode head){
        ListNode cur1 = first;
        ListNode cur2 = head;
        ListNode result = new ListNode();
        result = null;
        ListNode tail = null;
        ListNode next = null;
        while((cur1 != null)  &&  (cur2 != null)){
            if(cur1.data <= cur2.data){
                if(result != null){   //当结果链表不为空时
                    next = cur1.next;  // 保存链表1的下一个节点,让循环可以继续
                    tail.next = cur1;   // 插入过程
                    cur1.next = null;
                    tail = cur1;  //保存结果链表的最后一个节点
                    cur1 = next;
                }else{   // 结果链表为空时
                    next = cur1.next;
                    result = cur1;
                    cur1.next = null;
                    //保存新的最后一个节点
                    tail = cur1;
                    cur1 = next;

                }
            }else{
                if(result != null){
                    next = cur2.next;
                    tail.next = cur2;
                    cur2.next = null;
                    tail = cur2;
                    cur2 = next;
                }else{
                    next = cur2.next;
                    result = cur2;
                    cur2.next = null;
                    //保存新的最后一个节点
                    tail = cur2;
                    cur2 = next;
                    }
            }
        }
        //其中一个链表为空之后
            if(cur1 == null){
                tail.next = cur2;
            }
            if(cur2 == null){
                tail.next = cur1;
            }

        return result;
    }
    public static void print(ListNode head){
        while(head != null){
            System.out.println(head.data);
            head = head.next;
        }
   }
    public static void main(String[] args) {
        ListNode n1 = new ListNode();
        ListNode n2 = new ListNode();
        ListNode n3 = new ListNode();
        ListNode n4 = new ListNode();
        n1.data = 1;
        n2.data = 2;
        n3.data = 3;
        n4.data = 4;
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        ListNode n5 = new ListNode();
        ListNode n6 = new ListNode();
        ListNode n7 = new ListNode();
        ListNode n8 = new ListNode();
        n5.data = 1;
        n6.data = 6;
        n7.data = 7;
        n8.data = 8;
        n5.next = n6;
        n6.next = n7;
        n7.next = n8;
        MergeOrderedList(n1,n5);
        print(n1);
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值