Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点。比如345表示为链表5->4->3。而我们需要做的就是将两条链表代表的整数加起来,并创建一个新的链表并返回,新链表代表加总值。


这个问题与算法无关,其主要是操作链表这一数据结构,以及两个大数的加法。由于没有规定链表的长度,这意味着链表代表的整数可以任意大,这样就不能先利用链表计算出对应的整数值,之后利用加总值重新生成链表。我们必须手动处理两个大数的加法。

要谨慎两个个位数的加总结果是有可能需要进位的,即7+8实际上等于10x1+5,其中1应该进位到下一个结点,而5当作这个当前结点的值。

addTwoNumbers(Node n1, Node n2)

  res = Node{val = (n1.val + n2.val) % 10, next = null}

  tail = res

  adv = (n1.val + n2.val) / 10

  n1 = n1.next

  n2 = n2.next

  while n1 != null && n2 != null then

    sum = n1.val + n2.val + adv

    tail.next = Node{val = sum % 10, next = null}

    tail = tail.next

    adv = sum / 10

    n1 = n1.next

    n2 = n2.next

  Node notEndedNode = (n1 == null ? n2 : n1)

  while notEndedNode != null then

    sum = notEndedNode .val + adv

    tail.next = Node{val = sum % 10, next = null}

    tail = tail.next

    adv = sum / 10

    notEndedNode = notEndedNode.next

  while adv != 0 then

    tail.next = Node{val = adv % 10, next = null}

    tail = tail.next

    adv = adv / 10

  return res


下面直接给出java的实现代码

package cn.dalt.leetcode;

/**
 * Created by Administrator on 2017/6/4.
 */
public class AddTwoNumbers {

    public static void main(String[] args)
    {
        System.out.println(new AddTwoNumbers().addTwoNumbers(new ListNode(342), new ListNode(465)));
    }

    static class ListNode {
        ListNode(int x)
        {
            val = x % 10;
            int adv = x / 10;
            if(adv != 0)
            {
                next = new ListNode(adv);
            }
        }

        @Override
        public String toString() {
            return "" + val + (next != null ? "->" + next.toString() : "");
        }

        int val;
        ListNode next;
    }

    static class NodeList{
        ListNode tail;
        ListNode root;
        int advance = 0;
        public NodeList(int x)
        {
            root = new ListNode(x % 10);
            tail = root;
            advance = x / 10;
        }
        public void append(int x)
        {
            x += advance;
            ListNode temp = new ListNode(x % 10);
            advance = x / 10;

            tail.next = temp;
            tail = temp;
        }
        public ListNode getResult()
        {
            while(advance != 0)
            {
                append(0);
            }
            return root;
        }

        @Override
        public String toString() {
            return root.toString();
        }
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        NodeList list = new NodeList(l1.val + l2.val);
        l1 = l1.next;
        l2 = l2.next;

        while(l1 != null && l2 != null)
        {
            list.append(l1.val + l2.val);
            l1 = l1.next;
            l2 = l2.next;
        }
        while(l1 != null)
        {
            list.append(l1.val);
            l1 = l1.next;
        }
        while(l2 != null)
        {
            list.append(l2.val);
            l2 = l2.next;
        }
        return list.getResult();
    }
}

Leetcode:Add Two Numbers分析和实现的更多相关文章

  1. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  2. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  4. LeetCode: Add Two Numbers 解题报告

    Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...

  5. [LeetCode] Add Two Numbers题解

    Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...

  6. [Leetcode] Add two numbers 两数之和

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. [LeetCode] Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. LeetCode——Add Two Numbers

    Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...

  9. [LeetCode] Add Two Numbers 链表

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

随机推荐

  1. 一个简单的观察者模式的JS实现

    这不是原创文章,主要是写给自己看的.原文比较详细容易让人,我提取最简单最好理解的部分,便于我以后用到.参考http://www.cnblogs.com/TomXu/archive/2012/03/02 ...

  2. 收集前端UI框架 持续更新中....

    1. elementUI 饿了么出品 基于Vue2 http://element.eleme.io/#/zh-CN 2. ZUI 开源HTML5跨屏框架  (2018年1月4日更新)一个基于 Boot ...

  3. tensorflow中 tf.reduce_mean函数

    tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值. reduce_mean(input_ ...

  4. Mac Book Pro重新安装出错

    错误描述 未能创建用于apfs安装的预启动宗卷 解决 网上的经验: 返场重修 多试几次拼人品 多试了几次之后还是没用,选择U盘安装. 搞定! U盘安装教程

  5. 在SSH项目中实现分页效果

    在实现分页的时候,我使用的是数据库下面的User表,实现的效果是通过分页查询 能够将表中的数据分页显示,点击相关的按钮实现:首页.上一页.下一页.末页的显示 1新建一个dynamic web proj ...

  6. (精华)将json数组和对象转换成List和Map(小龙哥和牛徳鹤的对话)

    将java标准的数据结构ArrayList和HashMap转换成json对象和数组很简单 只需要JSONArray.fromObject(obj);或者JSONObject.fromObject(ob ...

  7. PyCharm永久激活

    目录 windws Mac Windows下破解 激活前准备工作 激活前请先关闭pycharm 修改配置文件的时候你需要填写你的安装路径 如果出现修改配置文件后无法打开pycharm,那就移动补丁的位 ...

  8. 深入理解Java虚拟机,gc输出参数

    https://blog.csdn.net/qq_21383435/article/details/80702205

  9. Zabbix 报警通知邮件和微信vim /etc/hosts

    1安装 sendmail # yum -y install sendmail echo 'This is test mail'>body.txt mail -s 'Test mail' 3013 ...

  10. Linux虚拟机与Windows共享文件

    1.找到“虚拟机” >>> “设置” 2.选项 >>> 共享文件夹 >>>  总是启用 >>> 添加,选择你想要共享的文件. 注 ...