前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 2 & 455 Add Two Numbers I&II

LeetCode 2 & 455 Add Two Numbers I&II

原创
作者头像
大学里的混子
修改2018-11-05 10:49:56
3950
修改2018-11-05 10:49:56
举报
文章被收录于专栏:LeetCodeLeetCode

2.Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order 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.

Example:

代码语言:javascript
复制
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

题目大意:输入两个链表,求和

解法:

代码语言:javascript
复制
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode cur1= l1;
            ListNode cur2= l2;
            ListNode head = new ListNode(0);
            ListNode cur = head;
            int sum = 0;
            while (cur1!= null || cur2 != null ){
                if(cur1!= null){
                    int val1 = cur1.val;
                    sum = sum +val1;
                    cur1 = cur1.next;
                }

                if(cur2!= null) {
                    int val2 = cur2.val;
                    sum = sum + val2;
                    cur2 = cur2.next;
                }
                cur.next = new ListNode( sum % 10);
                cur = cur.next;
                sum = sum/10;
            }
            if(sum == 1){
                cur.next = new ListNode(1);
            }
            return head.next;
        }

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:

代码语言:javascript
复制
Input:(7->2->4->3)+(5->6->4) 
Output:7->8->0->7

解法一:

代码语言:javascript
复制
   public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<Integer> stack1 = addTwoNumbers_stack(l1);
        Stack<Integer> stack2 = addTwoNumbers_stack(l2);
        Stack<Integer> stack3 = new Stack<>();
        ListNode dunny = new ListNode(0);
        ListNode cur = new ListNode(0);
        cur = dunny;
        int num1=0; int num2=0;int sum=0;
        int carry = 0 ;
        while (!stack1.isEmpty()||!stack2.isEmpty()){
            if (!stack1.isEmpty()){
                num1 = stack1.pop();
            }else num1=0;
            if (!stack2.isEmpty()){
                num2 = stack2.pop();
            }else num2 = 0;
            sum = (num1+num2+carry)%10;
            carry = (num1+num2+carry)/10;
            stack3.push(sum);
        }
        if (carry!=0) stack3.push(carry);
        while (!stack3.isEmpty()){
            cur.next =  new ListNode(stack3.pop());
            cur = cur.next;
        }
        return dunny.next;
    }

    public Stack<Integer> addTwoNumbers_stack(ListNode head){
        if (head == null ) return null;
        Stack<Integer> stack = new Stack<>();
        ListNode dunny = new ListNode(0);
        dunny.next = head;
        while (dunny.next!=null){
            stack.add(dunny.next.val);
            dunny = dunny.next;
        }
        return stack;
    }

解法二:

反转链表,虽然题目不允许反转链表,但是这种做法还是有很大优越性的,空间复杂度减小为O(1);

代码语言:javascript
复制
 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        l1 = reverseList(l1);
        l2 = reverseList(l2);
        ListNode res = addTwoNumbersii(l1,l2);
        l1 = reverseList(l1);
        l2 = reverseList(l2);
        return reverseList(res);
    }
    
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next ==null) return head;
        ListNode pre = null;
        ListNode cur = head;
        ListNode next;
        while (cur !=null){
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
    
    public ListNode addTwoNumbersii(ListNode l1, ListNode l2) {
    ListNode cur1= l1;
    ListNode cur2= l2;
    ListNode head = new ListNode(0);
    ListNode cur = head;
    int sum = 0;
    while (cur1!= null || cur2 != null ){    // 考虑到可能存在一个链表已经到达尾部
        if(cur1!= null){
            int val1 = cur1.val;
            sum = sum +val1;
            cur1 = cur1.next;
        }
        if(cur2!= null) {
            int val2 = cur2.val;
            sum = sum + val2;
            cur2 = cur2.next;
        }
        cur.next = new ListNode( sum % 10);
        cur = cur.next;
        sum = sum/10;
    }
    if(sum == 1){           //注意最高一位可能存在进位
        cur.next = new ListNode(1);
    }
    return head.next;
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2.Add Two Numbers
    • 解法:
    • 445.Add Two Numbers II
      • 解法一:
        • 解法二:
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档