1. You are given two linked lists representing two non-negative numbers. 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.
  2.  
  3. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
  4. Output: 7 -> 0 -> 8

两数之和(考察链表操作):

链表长度不一,有进位情况,最后的进位需要新增节点。

  1. int up = 0;
  2. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  3. if (l1 == null) {
  4. return l2;
  5. }
  6. if (l2 == null) {
  7. return l1;
  8. }
  9. ListNode newList = new ListNode(-1);
  10. ListNode newNode, rootList = newList;
  11. while (l1 != null && l2 != null) {
  12. newNode = new ListNode(l1.val + l2.val + up);
  13. up = newNode.val /10;
  14. newNode.val = mod(newNode.val,10, up);
  15. newList.next = newNode;
  16. newList = newList.next;
  17. l1 = l1.next;
  18. l2 = l2.next;
  19. }
  20. newList = loopList(l1, newList);
  21. newList = loopList(l2, newList);
  22. if (up > 0) {
  23. newList.next = new ListNode(up);
  24. }
  25. return rootList.next;
  26. }
  27.  
  28. ListNode loopList(ListNode loopList, ListNode newList) {
  29. ListNode newNode;
  30. while (loopList != null) {
  31. newNode = new ListNode(loopList.val + up);
  32. up = newNode.val /10;
  33. newNode.val = mod(newNode.val, 10, up);
  34. newList.next = newNode;
  35. newList = newList.next;
  36. loopList = loopList.next;
  37. }
  38. return newList;
  39. }
  40. static int mod(int x, int y, int v) {
  41. return x - y * v;
  42. }

码出来的代码不不如答案,哎,超级简洁:

  1. ListNode dummyHead = new ListNode(0);
  2. ListNode p = l1, q = l2, curr = dummyHead;
  3. int carry = 0;
  4. while (p != null || q != null) {
  5. int x = (p != null) ? p.val : 0;
  6. int y = (q != null) ? q.val : 0;
  7. int sum = carry + x + y;
  8. carry = sum / 10;
  9. curr.next = new ListNode(mod(sum , 10 , carry));
  10. curr = curr.next;
  11. if (p != null) p = p.next;
  12. if (q != null) q = q.next;
  13. }
  14. if (carry > 0) {
  15. curr.next = new ListNode(carry);
  16. }
  17. return dummyHead.next;

【leedcode】add-two-numbers的更多相关文章

  1. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  2. 【leetcode】Add Two Numbers

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

  3. 【题解】【链表】【Leetcode】Add Two Numbers

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

  4. 【leetcode】Add Two Numbers(middle) ☆

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

  5. 【leetcode】 Add Two Numbers

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

  6. 【链表】Add Two Numbers

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

  7. 【Leetcode】【Medium】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(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  9. 【LeetCode2】Add Two Numbers★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(2->4->3)(342) + (5-> ...

  10. 【LeetCode67】 Add Binary

    题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...

随机推荐

  1. 2.Java异常学习

    1.Java异常的概念 异常的例子 1.除法就是一个需要捕获异常的例子,除数又可能是0 异常处理的基本流程如下 一旦发生异常,就使得程序不按照原来的流程继续的运行下去 a.程序抛出异常 try{ th ...

  2. Android真机调试 Android.Util.AndroidRuntimeException: You cannot combine custom titles with other title features

    参考连接:http://blog.csdn.net/scyatcs/article/details/9003285 Android.Util.AndroidRuntimeException: You ...

  3. hdu1561 The more, The Better (树形dp+背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...

  4. 浏览器User-agent简史(user-agent)

    In the beginning there was NCSA Mosaic, and Mosaic called itself NCSA_Mosaic/2.0 (Windows 3.1), and ...

  5. inner join on 和 where = 的区别!

    请看下面两条语句:select * from table1 inner join table2 on table1.id = table2.idselect * from table1,table2 ...

  6. Linux琐碎

    本周接触Linux的内容: 1.netstat -tanlp 显示监听的所有端口并且不解析端口为属于哪个进程 history | grep cmd 从命令历史中找到需要的命令 2. scp命令的使用: ...

  7. Jenkins + Ant + Git + Tomcat自动化部署

    环境linux下,大致的配置内容如下: 首先安装JDK配置环境变量等. 其次安装ANT配置ANT_HONE并把bin目录加入PATH中. 然后安装Git,并生成sshkey配置ssh 安装tomcat ...

  8. Eclipse,到了说再见的时候了——Android Studio最全解析

    转自:http://blog.jobbole.com/77635/ 去年的Google大会上,Google带给我们一个小玩具——Android Studio,说它是玩具,是因为它确实比较菜,界面过时, ...

  9. Hint when use HTTPAgilityPack

    1- Read the usage policy of the website. I know this is the third time I mention that, but that tell ...

  10. java接口的嵌套

    java接口 1.接口中定义的变量默认是public static final 型,且必须给其初值,所以实现类中不能重新定义,也不能改变其值 2.接口中的方法默认都是 public abstract ...