两个单链表生成相加链表

【题目】 假设链表中每一个节点的值都在 0~9 之间,那么链表整体就可以代表一个整数。

例如:9->3->7,可以代表整数 937。

给定两个这种链表的头节点 head1 和 head2,请生成代表两个整数相加值的结果链表。

例如:链表 1 为 9->3->7,链表 2 为 6->3,最后生成新的结果链表为 1->0->0->0。

【解答】 这道题难度较低,考查面试者基本的代码实现能力。一种实现方式是将两个链表先算出各 自所代表的整数,然后求出两个整数的和,最后将这个和转换成链表的形式,但是这种方法有 一个很大的问题,链表的长度可以很长,可以表达一个很大的整数。因此,转成系统中的 int 类型时可能会溢出,所以不推荐这种方法。

方法一:利用栈结构求解。

1.将两个链表分别从左到右遍历,遍历过程中将值压栈,这样就生成了两个链表节点值的逆序栈,分别表示为 s1 和 s2。

例如:链表 9->3->7,s1 从栈顶到栈底为 7,3,9;链表 6->3,s2 从栈顶到栈底为 3,6。

2.将 s1 和 s2 同步弹出,这样就相当于两个链表从低位到高位依次弹出,在这个过程中生成相加链表即可,同时需要关注每一步是否有进位,用 ca 表示。

例如:s1 先弹出 7,s2 先弹出 3,这一步相加结果为 10,产生了进位,令 ca=1,然后生成 一个节点值为 0 的新节点,记为 new1;s1 再弹出 3,s2 再弹出 6,这时进位为 ca=1,所以这一 步相加结果为 10,继续产生进位,仍令 ca=1,然后生成一个节点值为 0 的新节点,记为 new2, 令 new2.next=new1;s1 再弹出 9,s2 为空,这时 ca=1,这一步相加结果为 10,仍令 ca=1,然 后生成一个节点值为 0 的新节点,记为 new3,令 new3.next=new2。这一步也是模拟简单的从 低位到高位进位相加的过程。

3.当 s1 和 s2 都为空时,还要关注一下进位信息是否为 1,如果为 1,比如步骤 2 中的例 子,表示还要生成一个节点值为 1 的新节点,记为 new4,令 new4.next=new3。

4.返回新生成的结果链表即可。

具体过程请参看如下代码中的 addLists1 方法。

public Node addList1(Node head1, Node head2) {
    if (head1 == null || head2 == null) {
        return null;
    }

    Stack<Node> s1 = new Stack<>();
    Stack<Node> s2 = new Stack<>();

    while (head1 != null) {
        s1.push(head1);
        head1 = head1.next;
    }

    while (head2 != null) {
        s2.push(head2);
        head2 = head2.next;
    }

    int ca = 0;
    int n1 = 0;
    int n2 = 0;
    int n = 0;

    Node pre = null;
    Node node = null;
    while (!s1.isEmpty() || !s2.isEmpty()) {
        n1 = s1.isEmpty() ? 0 : s1.pop().value;
        n2 = s2.isEmpty() ? 0 : s2.pop().value;

        n = n1 + n2 + ca;
        pre = node;
        node = new Node(n % 10);
        node.next = pre;
        ca = n / 10;
    }

    if (ca == 1) {
        pre = node;
        node = new Node(1);
        node.next = pre;
    }
    return node;
}

方法二:利用链表的逆序求解,可以节省用栈的空间。

1.将两个链表逆序,这样就可以依次得到从低位到高位的数字。

例如:链表 9->3->7,逆序后变为 7->3->9;链表 6->3,逆序后变为 3->6。

2.同步遍历两个逆序后的链表,这样就依次得到两个链表从低位到高位的数字,在这个过 程中生成相加链表即可,同时需要关注每一步是否有进位,用 ca 表示。具体过程与方法一的步 骤 2 相同。

3.当两个链表都遍历完成后,还要关注进位信息是否为 1,如果为 1,还要生成一个节点 值为 1 的新节点。

4.将两个逆序的链表再逆序一次,即调整成原来的样子。

5.返回新生成的结果链表。

具体过程请参看如下代码中的 addLists2 方法。

public Node addList2(Node head1, Node head2) {
    if (head1 == null || head2 == null) {
        return null;
    }

    // 1.将两个链表逆序
    head1 = reverseList(head1);
    head2 = reverseList(head2);
    // 记录逆序完之后的头节点,还原时要用
    Node h1 = head1;
    Node h2 = head2;

    int ca = 0;
    int n1 = 0;
    int n2 = 0;
    int n = 0;

    Node pre = null;
    Node node = null;

    // 2.生成新的链表
    while (head1 != null || head2 != null) {
        n1 = head1 != null ? head1.value : 0;
        n2 = head2 != null ? head2.value : 0;
        n = n1 + n2 + ca;
        pre = node;
        node = new Node(n % 10);
        node.next = pre;
        ca = n / 10;
        head1 = head1 != null ? head1.next : null;
        head2 = head2 != null ? head2.next : null;
    }

    if (ca == 1) {
        pre = node;
        node = new Node(1);
        node.next = pre;
    }

    // 还原 原来的链表
    reverseList(h1);
    reverseList(h2);

    return node;
}

public Node reverseList(Node head) {
    Node pre = null;
    Node next = null;

    while (head != null) {
        next = head.next;
        head.next = pre;
        pre = head;
        head = next;
    }
    return pre;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值