首先想到的是走到其中一个链表的尽头,然后把剩余的链表中的值放入写的链表,返回,但是自己写的代码好长。

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

Python:

  1. # Definition for singly-linked list.
  2. # class ListNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6.  
  7. class Solution(object):
  8. def addTwoNumbers(self, l1, l2):
  9. """
  10. :type l1: ListNode
  11. :type l2: ListNode
  12. :rtype: ListNode
  13. """
  14. tmp = l1
  15. up = 0
  16. while l1 and l2:
  17. l1.val += (l2.val+up)
  18. if l1.val > 9:
  19. up = 1
  20. l1.val %= 10
  21. else:
  22. up = 0
  23. p1,p2 = l1,l2
  24. l1,l2 = l1.next,l2.next
  25. if not l1 and not l2:
  26. if up:
  27. p1.next = ListNode(up)
  28. return tmp
  29. if l2:
  30. p1.next = l2
  31. l1 = p1.next
  32. while l1 and up:
  33. l1.val += up
  34. if l1.val >9:
  35. up=1
  36. l1.val %= 10
  37. else:
  38. up = 0
  39. p1 = l1
  40. l1 = l1.next
  41. if up:
  42. p1.next = ListNode(up)
  43. return tmp

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

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

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

  2. 【Leetcode】445. Add Two Numbers II

    You are given two non-empty linked lists representing two non-negative integers. The most significan ...

  3. 【LeetCode】002 Add Two Numbers

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

  4. 【LeetCode】2.Add Two Numbers 链表数相加

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

  5. 【LeetCode】2. Add Two Numbers 两数相加

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  6. 【LeetCode】165. Compare Version Numbers 解题报告(Python)

    [LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  7. 【LeetCode】623. Add One Row to Tree 解题报告(Python)

    [LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...

  8. 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...

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

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

随机推荐

  1. 【LeeetCode】4. Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  2. gridview动态添加行(不用datatable实现)

    GridViewRow newrow = , , DataControlRowType.Separator, DataControlRowState.Normal); TableCell[] tc=] ...

  3. Kafka集群搭建

    1.zookeeper搭建 Kafka集群依赖zookeeper,需要提前搭建好zookeeper zookeeper快速搭建推荐地址:http://nileader.blog.51cto.com/1 ...

  4. 使用Stardict命令行版本sdcv

    sdcv命令的常用选项如下: -l:列出安装的词典 -u:指定查词所用的词典 在我的电脑上列出的词典有: Dictionary's name Word count Merrian Webster 10 ...

  5. UGUI和现实世界的比例关系

    之前测试过默认大小的 Cube 在现实中的 比例关系,得出基本单位为 m 的结论,至于 UGUI和现实世界的比例关系 看下图就知道了: Cube Collider 的大小: Button 的大小: 其 ...

  6. Linux连接xshell找不到IP信息

    虚拟机环境下的Linux连接xshell的网络连接找不到eth0(IP)信息的解决方法   1  输入ifconfig,如果有eth0信息,直接填写eth0上面的IP信息   2 输入ifconfig ...

  7. CentOS6.5部署L2TP over IPSec

    一.环境介绍: 1.CentOS 6.5 (要求双网卡做软路由,如果只是做VPN可以单网卡) a.外网IP: b.内网IP: 2.Window 10 主机一台做为一台内网测试软路由使用: a.内网IP ...

  8. Lua 数据类型和 Redis 数据类型之间转换

    当 Lua 通过 call() 或 pcall() 函数执行 Redis 命令的时候,命令的返回值会被转换成 Lua 数据结构. 同样地,当 Lua 脚本在 Redis 内置的解释器里运行时,Lua ...

  9. Jedis实现发布订阅功能

    Redis为我们提供了publish/subscribe(发布/订阅)功能.我们可以对某个channel(频道)进行subscribe(订阅),当有人在这个channel上publish(发布)消息时 ...

  10. box-sizing的不同属性值间的区别

    box-sizing:值为 border-box时,其含义为:表示元素的宽度与高度包括内部补白区域(指border和padding)与边框的宽度与高度:值为content-box时,其含义正其前者相反 ...