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,则要再增加一位。

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * struct ListNode *next;
  6. * };
  7. */
  8. struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
  9. struct ListNode *Ans, *TempNode, *TailNode;
  10. int Carry;
  11.  
  12. Carry = ;
  13. Ans = TailNode = NULL;
  14. while(NULL != l1 && NULL != l2){
  15. TempNode = (struct ListNode*)malloc(sizeof(struct ListNode));
  16. TempNode->val = (Carry + l1->val + l2->val) % ;
  17. TempNode->next = NULL;
  18. Carry = (Carry + l1->val + l2->val) / ;
  19. if(NULL == Ans){
  20. Ans = TempNode;
  21. TailNode = TempNode;
  22. }
  23. else{
  24. TailNode->next = TempNode;
  25. TailNode = TailNode->next;
  26. }
  27. l1 = l1->next;
  28. l2 = l2->next;
  29. }
  30. while(NULL != l1){
  31. TempNode = (struct ListNode*)malloc(sizeof(struct ListNode));
  32. TempNode->val = (Carry + l1->val) % ;
  33. TempNode->next = NULL;
  34. Carry = (Carry + l1->val) / ;
  35. TailNode->next = TempNode;
  36. TailNode = TailNode->next;
  37. l1 = l1->next;
  38. }
  39. while(NULL != l2){
  40. TempNode = (struct ListNode*)malloc(sizeof(struct ListNode));
  41. TempNode->val = (Carry + l2->val) % ;
  42. TempNode->next = NULL;
  43. Carry = (Carry + l2->val) / ;
  44. TailNode->next = TempNode;
  45. TailNode = TailNode->next;
  46. l2 = l2->next;
  47. }
  48. != Carry){
  49. TempNode = (struct ListNode*)malloc(sizeof(struct ListNode));
  50. TempNode->val = Carry;
  51. TempNode->next = NULL;
  52. TailNode->next = TempNode;
  53. }
  54.  
  55. return Ans;
  56. }

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. ctags使用

    1:安装ctags sudo apt-get install exuberant-ctags ctags --help 2:建立源码之间的组织关系: 1:ctags ./*.c -R 生成tags文件 ...

  2. gcc 工作流程

    gcc常用参数: 1:-v/--version 2:-I :指定头文件包含路径 3:-c :将汇编文件生成一个二级制文件,得到.o文件 4:-o :指定生产的文件名 5:-g :gdb调试的时候需要加 ...

  3. centos下部署启动elasticsearch错误集合与解决方案

    问题一: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] 解决步 ...

  4. C# .NET 配置404,500等错误信息

    <customErrors mode="On" defaultRedirect="viewAll.html"><!--所有的错误显示页--&g ...

  5. tomcat和eclipse-wtp的一些配置

    TOMCAT配置UTF-8 server.xml配置文件: <Connector port="8080" protocol="HTTP/1.1" conn ...

  6. 轻松解决oracle11g 空表不能exp导出的问题。

    解决方法: 1插入一条数据(或者再删除),浪费时间,有时几百张表会累死的.2创建数据库之前使用代码: Sql代码 alter system set  deferred_segment_creation ...

  7. 'git status'不显示untracked files

    git status -uno git status --untracked-files=no

  8. 实用的DDos攻击工具

    来源: http://www.safecdn.cn/linux/2018/12/ddos/95.html ‎ 特别提示:仅用于攻防演练及教学测试用途,禁止非法使用 Hyenae 是在windows平台 ...

  9. javascript中的立即执行函数(function(){…})()

    javascript中的立即执行函数(function(){…})() 深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是用(function(){…})()包 ...

  10. H5自动准备杂记

    由于之前没做过UI自动化,近期准备做H5自动化,要学的东西还是很多. 1.本地debug环境:android studio + android SDK(想要调试通要关注:驱动.手机开发者模式要打开) ...