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. iOS开发Objective-C基础之──多态

    Objective-C语言是面向对象的高级编程语言,因此,它具有面向对象编程所具有的一些特性,即:封装性.继承性和多态性. 今天介绍一下Objective-C中的多态性. 一.什么是多态 多态:不同对 ...

  2. cell 重用

    1. 当单元格因滚屏而脱落表格可见区时,表格可以将其缓存到重用队列中. 用法:我们可标记单元格以备重用,然后根据需要从该队列中提取. 在分配新单元格时,必须检查重用单元格是否可用.如果表格对deque ...

  3. synchronized一个(二)

    今天遇到了一个关于synchronized的一个问题,关于其持有锁的问题.这个问题以前是有看过相关文章的,但是一直没有记录,今天大概记录一下当前的认知. 对于静态方法,synchronized的使用的 ...

  4. CAM350对比两个gerber之间的差异

    今天客供的gerber 版本更新,要检查区别. 参考: https://wenku.baidu.com/view/a154028c19e8b8f67d1cb93f.html 这个更加详细: https ...

  5. Chrome浏览器中使用 iframe 嵌入网页导致视频不能全屏的问题解决方法

    今天无意中测试了下在 iframe 中嵌入视频, 发现全屏按钮在 Chrome 浏览器中居然无效, 试了好几个视频网站的视频都不能全屏, 但在其他浏览器中似乎都很正常, 应该是 Chrome 60 新 ...

  6. bag of words

    参考文献 Bag-of-words model (BoW model) 最早出现在NLP和IR领域. 该模型忽略掉文本的语法和语序, 用一组无序的单词(words)来表达一段文字或一个文档. 近年来, ...

  7. TCP/IP详解与OSI七层模型

    TCP/IP协议 包含了一系列构成互联网基础的网络协议,是Internet的核心协议.基于TCP/IP的参考模型将协议分成四个层次,它们分别是链路层.网络层.传输层和应用层.下图表示TCP/IP模型与 ...

  8. baby用品

    新生嬰兒用品清單 1.哺育用品: 大奶瓶:6支,240ml左右.選擇PC材質耐高溫120度,可消毒:玻璃材質建議選用印刷安全無鉛材料,可消毒. 小奶瓶:2-3支,120ml左右.寬口徑/一般口徑(喝水 ...

  9. Tomcat服务器端口的配置

    一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. 如果想修改Tomcat服务器的启动端口,则可以在server.xml ...

  10. TCP heart

    http://blog.csdn.net/lisonglisonglisong/article/details/51327695