445. Add Two Numbers II [Medium]

本文介绍两种解决链表加法的方法:一种是反转链表后进行加法运算;另一种是使用栈来实现从尾部开始的加法。这两种方法都能有效地处理链表节点的进位问题。

 考虑:

因为要进位,应该只能从后往前计算

思路1:先revserse两个list再相加,题目变成普通的Add Two Numbers

思路2:用stack先把两个list都push完,再同步pop得到从后往前相加节点的效果

/**
 * 思路1,先reverse链表再当作Add Two Numbers做
 * Runtime: 2 ms, faster than 98.01%
 * Memory Usage: 39.5 MB, less than 45.90%
 */
class Solution {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        // reverse lists
        ListNode l1New = reverseList(l1);
        ListNode l2New = reverseList(l2);
        
        ListNode sentinel = new ListNode(-1);
        int carry = 0;
        // deal with common part in two lists
        while (l1New != null && l2New != null) {
            int sum = l1New.val + l2New.val + carry;
            carry = sum / 10;
            ListNode curr = new ListNode(sum % 10);
            curr.next = sentinel.next;
            sentinel.next = curr;
            l1New = l1New.next;
            l2New = l2New.next;
        }
        // deal with the rest part in the longer list
        ListNode next = l2New == null ? l1New : l2New;
        while (next != null) {
            int sum = next.val + carry;
            carry = sum / 10;
            ListNode curr = new ListNode(sum % 10);
            curr.next = sentinel.next;
            sentinel.next = curr;
            next = next.next;
        }
        // deal with the last carry bit
        if (carry == 1) {
            ListNode curr = new ListNode(1);
            curr.next = sentinel.next;
            sentinel.next = curr;
        }
        return sentinel.next;
    }
    
    private ListNode reverseList(ListNode head) {
        ListNode sentinel = new ListNode(-1);
        while (head != null) {
            ListNode next = head.next;
            head.next = sentinel.next;
            sentinel.next = head;
            head = next;
        }
        return sentinel.next;
    }

}
/**
 * 也是思路1,简化了上一段代码,将l1New、l2New都不为空的情况,和其中一个为空的情况合并
 * Runtime: 2 ms, faster than 98.01%
 * Memory Usage: 39 MB, less than 94.90% 
 */
class Solution {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        // reverse lists
        ListNode l1New = reverseList(l1);
        ListNode l2New = reverseList(l2);
        
        ListNode sentinel = new ListNode(-1);
        int carry = 0;
        // deal with all nodes in two lists
        while (l1New != null || l2New != null) {
            int sum = (l1New == null ? 0 : l1New.val) + (l2New == null ? 0 : l2New.val) + carry;
            carry = sum / 10;
            ListNode curr = new ListNode(sum % 10);
            curr.next = sentinel.next;
            sentinel.next = curr;
            l1New = l1New == null ? null : l1New.next;
            l2New = l2New == null ? null : l2New.next;
        }
        // deal with the last carry bit
        if (carry == 1) {
            ListNode curr = new ListNode(1);
            curr.next = sentinel.next;
            sentinel.next = curr;
        }
        return sentinel.next;
    }
    
    private ListNode reverseList(ListNode head) {
        ListNode sentinel = new ListNode(-1);
        while (head != null) {
            ListNode next = head.next;
            head.next = sentinel.next;
            sentinel.next = head;
            head = next;
        }
        return sentinel.next;
    }

}
/**
 * 思路2,先将两个链表全部push到两个栈,再从栈中同步取节点当作Add Two Numbers做
 * Runtime: 2 ms, faster than 98.01%
 * Memory Usage: 39.2 MB, less than 69.42%
 */
class Solution {
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode sentinel = new ListNode(-1);
        int carry = 0;
        LinkedList<ListNode> s1 = new LinkedList();
        LinkedList<ListNode> s2 = new LinkedList();
        // construct two stacks
        while (l1 != null) {
            s1.push(l1);
            l1 = l1.next;
        }
        while (l2 != null) {
            s2.push(l2);
            l2 = l2.next;
        }
        // deal with all nodes in two lists
        while (!s1.isEmpty() || !s2.isEmpty()) {
            int sum = (s1.isEmpty() ? 0 : s1.pop().val) + (s2.isEmpty() ? 0 : s2.pop().val) + carry;
            carry = sum / 10;
            ListNode curr = new ListNode(sum % 10);
            curr.next = sentinel.next;
            sentinel.next = curr;
        }
        // deal with the last carry bit
        if (carry == 1) {
            ListNode curr = new ListNode(1);
            curr.next = sentinel.next;
            sentinel.next = curr;
        }
        
        return sentinel.next;
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值