两个有序链表的合并

目录

题目一:两个有序链表的合并

题目二:链表相加


题目一两个有序链表的合并

给定两个有序链表的头节点head1和head2,

返回合并之后的大链表,要求依然有序

例子1-> 3-> 3-> 5-> 7     2 -> 2-> 3->3->7

返回1->2->2->3-> 3-> 3-> 3->5->7

思路

  • 确认头节点head;
  • 用指针cur1和cur2分别跟随两个链表;
  • 另一个头指针pre指向cur1和cur2中较小的那个,然后cur(较小)和pre后移(next);
public static Node mergeOrderList (Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return head1 == null ? head2 : head1;
        }
        Node head = head1.value <= head2.value ? head1 :head2;
        Node cur1 = head.next;
        Node cur2 = head == head1 ? head2 :head1;
        Node pre = head;
        while (cur1 != null && cur2 != null) {
            if (cur1.value <= cur2.value) {
                pre.next = cur1;
                cur1 = cur1.next;
            } else {
                pre.next = cur2;
                cur2 = cur2.next;
            }
            pre = pre.next;
        }
        pre.next = cur1 == null ? cur2 :cur1;
        return head;
    }

测试

    public static void main(String[] args) {
        Node n1 = new Node(2);
        n1.next = new Node(4);
        n1.next.next = new Node(11);

        Node n2 = new Node(1);
        n2.next = new Node(3);
        n2.next.next = new Node(5);

        Node n3 = mergeOrderList(n1,n2);
        while (n3 != null) {
            System.out.print(n3.value+" ");
            n3 = n3.next;
    }

输出

1 2 3 4 5 11 

题目二:链表相加

给定两个链表的头节点head1和head2,

认为从左到右是某个数字从低位到高位,返回相加之后的链表

例子        4->3->62-> 5-> 3

返回        6-> 8 -> 9

解释        634+ 352 = 986

思路:

  • 记录进位carry,本位curNum
  • 以较短链表为基准
  • 以较长链表的剩余部分为基准
  • 当两个链表都遍历结束时,判断最后是否有一个进位
    
    //计算链表长度
    public static int Length (Node head) {
        if (head == null) {
            return 0;
        }
        int length = 0;
        while (head != null) {
            length++;
            head = head.next;
        }
        return length;
    }
    //主方法
    public static Node AddList (Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return head1 == null ? head2 : head1;
        }
        Node l = Length(head1) >= Length(head2) ? head1 :head2;
        Node s = l.value== head1.value ? head2 :head1;
        Node head = l;
        int carry = 0;
        int curNum = 0;
        Node lastNode = null;
        while (s != null) {
            curNum = s.value + l.value + carry;
            l.value = curNum % 10;
            carry = curNum / 10;
            lastNode = l;
            l = l.next;
            s = s.next;
        }

        while (l != null) {
            curNum = l.value + carry;
            l.value = curNum % 10;
            carry = curNum / 10;
            lastNode = l;
            l = l.next;
        }
        if (carry != 0) {
            lastNode.next = new Node(1);
        }
        return head;
    }
    

测试

    public static void main(String[] args) {
        Node n1 = new Node(2);
        n1.next = new Node(4);
        n1.next.next = new Node(8);

        Node n2 = new Node(1);
        n2.next = new Node(3);
        n2.next.next = new Node(5);
        Node n4 = null;

        Node n3 = AddList(n1,n2);
        while (n3 != null) {
            System.out.print(n3.value+" ");
            n3 = n3.next;
        }
    }

输出

3 7 3 1 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

superzheng__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值