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.

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

解答

寒假刷的第一题……

相当于是实现一个全加器阵列……大概是还没有走出数逻的阴影……

当然局部变量要记得初始化,将Carry变量赋值为0,这样链表头节点的相加就可以归入链表中一般节点的相加,将Ans赋值为NULL,为循环中链表的插入做准备,每一位都要加上前一位Carry的值并重新计算Carry,第一位的前一位Carry的值是0,最后一位相加也要重新计算Carry,并且如果两个链表长度不同,程序进行到后面只对一个链表进行计算时,每一位也要加上前一位Carry的值并重新计算Carry,因为可能存在9+999999的情况,其实这样的情况也就相当于是一个全加器单元的一个加数是0,并且最后一位相加结束后Carry不为0,则要再增加一位。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *Ans, *TempNode, *TailNode;
    int Carry;

    Carry = ;
    Ans = TailNode = NULL;
    while(NULL != l1 && NULL != l2){
        TempNode = (struct ListNode*)malloc(sizeof(struct ListNode));
        TempNode->val = (Carry + l1->val + l2->val) % ;
        TempNode->next = NULL;
        Carry = (Carry + l1->val + l2->val) / ;
        if(NULL == Ans){
            Ans = TempNode;
            TailNode = TempNode;
        }
        else{
            TailNode->next = TempNode;
            TailNode = TailNode->next;
        }
        l1 = l1->next;
        l2 = l2->next;
    }
    while(NULL != l1){
        TempNode = (struct ListNode*)malloc(sizeof(struct ListNode));
        TempNode->val = (Carry + l1->val) % ;
        TempNode->next = NULL;
        Carry = (Carry + l1->val) / ;
        TailNode->next = TempNode;
        TailNode = TailNode->next;
        l1 = l1->next;
    }
    while(NULL != l2){
        TempNode = (struct ListNode*)malloc(sizeof(struct ListNode));
        TempNode->val = (Carry + l2->val) % ;
        TempNode->next = NULL;
        Carry = (Carry + l2->val) / ;
        TailNode->next = TempNode;
        TailNode = TailNode->next;
        l2 = l2->next;
    }
     != Carry){
        TempNode = (struct ListNode*)malloc(sizeof(struct ListNode));
        TempNode->val = Carry;
        TempNode->next = NULL;
        TailNode->next = TempNode;
    }

    return Ans;
}

LeetCode OJ 2. Add Two Numbers的更多相关文章

  1. 【LeetCode OJ】Add Two Numbers

    题目:You are given two linked lists representing two non-negative numbers. The digits are stored in re ...

  2. LeetCode OJ: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 java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  4. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  5. 《LeetBook》LeetCode题解(2):Add Two Numbers [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. C# 写 LeetCode Medium #2 Add Two Numbers

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

  7. LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表

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

  8. leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)

    https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...

  9. 【一天一道leetcode】 #2 Add Two Numbers

    一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...

随机推荐

  1. 知识点:linux数据库备份

    服务端启用二进制日志 如果日志没有启开,必须启用binlog,要重启mysql,首先,关闭mysql,打开/etc/my.cnf,加入以下几行: [mysqld] log-bin 然后重新启动mysq ...

  2. c#语言函数

    class Program {访问修饰符 函数名(参数1,参数2){   函数体   return 返回值} 无参数,无返回值 public static void abc()             ...

  3. 17 RAID与mdadm管理命令

    在"14 磁盘及文件系统管理详解"中,我们详细介绍了磁盘的工作原理,但是,有一点我们一定要明白,作为现在存储数据的主要设备,机械磁盘早就是上个世纪的产品,而它的读写速度与内存.CP ...

  4. 如何在maven项目里面编写mapreduce程序以及一个maven项目里面管理多个mapreduce程序

    我们平时创建普通的mapreduce项目,在遍代码当你需要导包使用一些工具类的时候, 你需要自己找到对应的架包,再导进项目里面其实这样做非常不方便,我建议我们还是用maven项目来得方便多了 话不多说 ...

  5. 初级安全入门——Windows操作系统的安全加固

    实验网络拓扑如下: 工具简介 Kali操作系统 Kali Linux是安全业内最知名的安全渗透测试专用操作系统.它的前身就是业界知名的BackTrack操作系统.BackTrack在2013年停止更新 ...

  6. QT_QSlider的总结

    当鼠标选中QSlider 上时,通过点击的数值为setpageStep():通过左右方向键按钮移动的数值为setsingleStep(). 鼠标滚轮上面两者都不行,不知道是什么原因! 应用: http ...

  7. LeetCode 失败的尝试 10. regular expression matching & 正则

    Regular Expression Matching 看到正则就感觉头大,因为正则用好了就很强大.有挑战的才有意思. 其实没有一点思路.循环的话,不能一一对比,匹配模式解释的是之前的字符.那就先遍历 ...

  8. gentoo wireshark 安装

    安装软件 emerge --ask net-analyzer/wireshark 把用户加入 wireshark 组. gpasswd -a $USER wireshark 如果不像重新登录就可以使用 ...

  9. Eclipse properties.config.yml 配置文件中文编码问题。Eclipse 配置文件插件解决方案

    写了中文默认转成unicode. 正常应该是这样子的 其实不是什么大问题只需要装一个插件就行了,插件有很多.推荐使用,Properties Editor 安装方式如下 .这里使用的是离线安装.即本地文 ...

  10. 获取手机浏览器IP的函数

    function GetIP() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //如果变量是非空或非零的值,则 empty()返回 FALSE. $IP = ...