两个单链表生成相加链表

【题目】 假设链表中每一个节点的值都在 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;
}
(1)在Java中,将单链表按基准划分通常涉及到双指针法。例如,可以定义两个指针,一个快指针先走一步,另一个慢指针每次走一步,当快指针到达基准点时,慢指针的位置就是分界线。然后分别将链表前半部分和后半部分连接起来。以下是简单的示例: ```java public class ListNode { int val; ListNode next; // constructor and getters/setters... } ListNode partition(ListNode head, int k) { if (head == null || head.next == null || k <= 0) return head; ListNode dummy = new ListNode(0); dummy.next = head; ListNode slow = dummy; ListNode fast = dummy.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } ListNode pivot = slow.next; // 分割点 slow.next = null; // 断开后半部分 ListNode left = dummy.next; ListNode right = pivot; return merge(left, right); // 合并左右两部分 } ListNode merge(ListNode l1, ListNode l2) { // ... 实现合并操作 } ``` (2)合并两个单链表可以通过逐节点比较它们的值,将较小的节点添到结果链表,直到其中一个链表遍历完。然后将剩余的链表接上。伪代码如下: ```java ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) return l2; if (l2 == null) return l1; if (l1.val < l2.val) { l1.next = mergeTwoLists(l1.next, l2); return l1; } else { l2.next = mergeTwoLists(l1, l2.next); return l2; } } ``` (3)集合运算如并集、交集和差集可以借助哈希表来提高效率。首先将每个链表转换成哈希集合,然后进行计算。这里给出简化的描述: - 并集:取两个集合的元素集合 - 交集:找出同时存在于两个集合中的元素 - 差集:在第一个集合中减去第二个集合的元素 ```java HashSet<Integer> set1 = toHashSet(list1); HashSet<Integer> set2 = toHashSet(list2); // 并集:set1.addAll(set2) // 交集:set1.retainAll(set2) // 差集:set1.removeAll(set2) ``` 其中`toHashSet()`函数用于将链表转换为哈希集合。 (4)两个多项式相加,需要对对应系数进行累,并考虑进位。可以定义一个节点类存储系数和指数,然后逐项相加。示例代码: ```java class Polynomial { List<Pair<Integer, Integer>> terms; // ... 添相加等方法 } Pair<Integer, Integer> addTerm(Pair<Integer, Integer> a, Pair<Integer, Integer> b) { int sumCoef = a.getKey() + b.getKey(); int maxExponent = Math.max(a.getValue(), b.getValue()); return new Pair<>(sumCoef, maxExponent); } Polynomial add(Polynomial p1, Polynomial p2) { // ... 实现合并两个多项式的操作 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值