445. Add Two Numbers II

题目描述

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7

给定两个非空链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储单个数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
进阶:
如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。
示例:

输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 8 -> 0 -> 7

作者:烛火的咆哮
链接:https://www.jianshu.com/p/551fa73bacb3
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

方法思路

1.毫无疑问,难点在于进位的处理,不需要进位的情况下,仅仅只是简单的同位相加
2.由于链表无法逆向遍历,需要通过其他手段辅助定位,如:链表反转,集合. 也可以通过转为10进制进行操作, 不过这也是效率较低的选择(注意溢出)
3.在不知晓链表长度情况下,重建链表的效果要高于在原立案表上进行改动

Apporach1:反转链表

class Solution {
    //Runtime: 20 ms, faster than 99.91%
    //Memory Usage: 47.6 MB, less than 34.75% 
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode root1 = reverse(l1);
        ListNode root2 = reverse(l2);
        
        ListNode root = null, current = null;
        int bits = 0;
        ListNode n1 = root1, n2 = root2;
        for(;n1 != null && n2 != null;n1 = n1.next, n2 = n2.next) {
            int value = n1.val + n2.val + bits;
            bits = value / 10;
            
            if(root == null) {
                root = new ListNode(value % 10);//root为新建链表的首部
                current = root;
            }else {
                current.next = new ListNode(value % 10);//重建链表
                current = current.next;
            }
        }
        
        for(;n1 != null;n1 = n1.next) {
            int value = n1.val + bits;
            bits = value / 10;
            
            current.next = new ListNode(value % 10);//重建链表
            current = current.next;
        }
        
        for(;n2 != null;n2 = n2.next) {
            int value = n2.val + bits;
            bits = value / 10;
            
            current.next = new ListNode(value % 10);//重建链表
            current = current.next;
        }
        
        if(bits != 0) {
            current.next = new ListNode(bits);//重建链表
            current = current.next;
        }
        return reverse(root);
    }
    //反转方法
    public ListNode reverse(ListNode head){
        if(head == null && head.next == null)
            return head;
        
        ListNode prev = null, cur = head;
        while(cur != null){
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
}

Approach2:Java Solution using Stack

使用栈进行操作,通过出栈操作达到逆向遍历链表的目的
此处也可以使用list等其他存储集合,但是效率都很低
public class Solution {
    //O(n) Java Solution using Stack
    //Runtime: 25 ms, faster than 77.39%
    //Memory Usage: 47.4 MB, less than 37.23%
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //栈是Vector的一个子类,它实现了一个标准的后进先出的栈。
        Stack<Integer> s1 = new Stack<Integer>();
        Stack<Integer> s2 = new Stack<Integer>();
        
        while(l1 != null) {
            s1.push(l1.val);//入栈
            l1 = l1.next;
        };
        while(l2 != null) {
            s2.push(l2.val);
            l2 = l2.next;
        }
        
        int sum = 0;
        ListNode list = new ListNode(0);
        while (!s1.empty() || !s2.empty()) {
            if (!s1.empty()) sum += s1.pop();//pop() 出栈
            if (!s2.empty()) sum += s2.pop();
            list.val = sum % 10;
            ListNode head = new ListNode(sum / 10);//若两个链表长度相同,且最高位需要进位
            head.next = list;
            list = head;
            sum /= 10;//若进位,怎用于下一高位
        }
        
        //若两个链表长度相同,且最高位需要进位,则return list;否则return list.next
        return list.val == 0 ? list.next : list;
    }
}

Approach3: 上面两种方法的结合 栈+反转

public class Solution {
    //O(n) Java Solution using Stack
    //Runtime: 24 ms, faster than 82.72% 
    //Memory Usage: 44.4 MB, less than 57.08% 
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //栈是Vector的一个子类,它实现了一个标准的后进先出的栈。
        Stack<Integer> s1 = new Stack<Integer>();
        Stack<Integer> s2 = new Stack<Integer>();
        
        while(l1 != null) {
            s1.push(l1.val);//入栈
            l1 = l1.next;
        };
        while(l2 != null) {
            s2.push(l2.val);
            l2 = l2.next;
        }
        
        int sum = 0;
        ListNode list = null, cur = null;
        while (!s1.empty() || !s2.empty()) {
            if (!s1.empty()) sum += s1.pop();//pop() 出栈
            if (!s2.empty()) sum += s2.pop();
            if(list == null){
                list = new ListNode(sum % 10);
                cur = list;
            }else{
                cur.next = new ListNode(sum % 10);
                cur = cur.next;
            }
            sum /= 10;//若进位,怎用于下一高位
        }
        
        if(sum != 0){
           cur.next = new ListNode(sum % 10);
                cur = cur.next; 
        }
        
        return reverse(list);
    }
    
    public ListNode reverse(ListNode head){
        if(head == null && head.next == null)
            return head;
        
        ListNode prev = null, cur = head;
        while(cur != null){
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }
}

总结

  1. 本题主要考察当顺序遍历无法满足结题要求时,对数据的处理方法
  2. 逆向链表与栈都是非常好的方法,若使用转为十进制进行操作,需要处理数值溢出问题2.逆向链表与栈都是非常好的方法,若使用转为十进制进行操作,需要处理数值溢出问题
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值