Middle-题目110:2. Add Two Numbers

原创 2016年05月31日 20:00:40

题目原文:
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
题目大意:
给出两个数,用链表逆序的存储每个数位,求他们的和,并也用链表逆序存储数位。
题目分析:
方法一:用BigInteger储存两个数字,再加起来,再生成对应链表。
方法二:模拟整数竖式算法(好在是逆序的, 正序就麻烦了)
源码:(language:java/c)
方法一:

import java.math.BigInteger;

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        BigInteger num1 = BigInteger.ZERO;
        BigInteger num2 = BigInteger.ZERO;
        BigInteger temp = BigInteger.ONE;

        while(l1!=null) {
            //num1+=temp*l1.val;
            num1=num1.add(temp.multiply(BigInteger.valueOf(l1.val)));
            temp=temp.multiply(BigInteger.TEN);
            l1=l1.next;
        }
        temp=BigInteger.ONE;
        while(l2!=null) {
            num2=num2.add(temp.multiply(BigInteger.valueOf(l2.val)));
            temp=temp.multiply(BigInteger.TEN);
            l2=l2.next;
        }
        BigInteger sum = num1.add(num2);
        if(sum.compareTo(BigInteger.ZERO)==0)
            return new ListNode(0);
        else {
            ListNode l = new ListNode(sum.mod(BigInteger.TEN).intValue());
            ListNode p=l;
            while(sum.compareTo(BigInteger.ZERO)!=0) {
                sum=sum.divide(BigInteger.TEN);
                if(sum.compareTo(BigInteger.ZERO)!=0) {
                    p.next = new ListNode(sum.mod(BigInteger.TEN).intValue());
                    p=p.next;
                }
            }
            return l;
        }
    }
}

方法二:(from discuss)

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode* result = l1 ? l1 : l2;
    struct ListNode* carry = l1 ? l2 : l1;
    struct ListNode* node = result;
    struct ListNode* tmp;
    int c = 0;
    int s;
    while (l1 || l2) {
        s = c;
        if (l1) { s += l1->val; l1=l1->next; }
        if (l2) { s += l2->val; l2=l2->next; }
        c = s > 9 ? 1 : 0;
        node->val = c ? s - 10 : s;
        if (l1) {
            node = node->next = l1;
        } else if (l2) {
            node = node->next = l2;
        } else {
            node->next = NULL;
        }
    }
    if (c) {
        carry->val = c;
        node->next = carry;
        node = node->next;
    }
    node->next = NULL;
    return result;
}

成绩:
方法一:28ms,beats 1.57%,众数4ms,63.50%
方法二:20ms,beats 18.86%,众数20ms,68.68%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

LeetCode 作业题目:2. Add Two Numbers

题目原址:点击打开链接  题目描述: You are given two non-empty linked lists representing two non-negative intege...

【python】【leetcode】【算法题目2—Add Two Numbers】

【python】【leetcode】【算法题目2—Add Two Numbers】 题目原文: You are given two linked lists representing two non-...

Leetcode: Add Two Numbers (2)

You are given two linked lists representing two non-negative numbers. The digits are stored in orde...
  • lzmaths
  • lzmaths
  • 2013年12月18日 14:19
  • 376

LeetCode Algorithms 2. Add Two Numbers 题解

题目: You are given two non-empty linked lists representing two non-negative integers. The digits...

LeetCode#2 Add Two Numbers

题目:You are given two non-empty linked lists representing two non-negative integers. The digits are s...

leetcode 2. Add Two Numbers

1.题目描述                                                                                              ...

算法设计Week1 LeetCode Algorithms Problem #2 Add Two Numbers

题目描述 You are given two non-empty linked lists representing two non-negative integers. The digits...

leetCode 2 Add Two Numbers

问题:Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits...

Leetcode 2. Add Two Numbers

leetcode页面样式换了,标题和total accepted也截不到了。就直接粘描述吧 You are given two linked lists representing two non-n...

leetcode #2 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in rever...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Middle-题目110:2. Add Two Numbers
举报原因:
原因补充:

(最多只允许输入30个字)