LeetCode02:Add Two Numbers Java实现

来源:互联网 发布:葫芦丝价格淘宝 编辑:程序博客网 时间:2024/05/13 02:34
原题目:
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.
 
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
给定两个包含正数的链表,这个链表只能存储个位数,以链表的形式返回这个链表各位相加的和。

算法分析:
这是个只能保存个位的链表,即在相加的过程中可能出现进位,例如:9->9->9链表和8这个链表相加的结果应为0->0->0->1,所以应当重新定义一个链表。
不能将两个链表相加的和作为L1或L2链表返回,因为这要会很麻烦,应该重新建立一个链表,保存每个节点相加和进位的和。最后应该判断,进位标志位是否任然不为0,这时应该继续创建新的节点,并将进位标志作为节点的值。
 
LeetCode提交源码:
  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. public class Solution {
  10. public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  11. if(l1 == null){
  12. return l2;
  13. }
  14. if(l2 == null){
  15. return l1;
  16. }
  17. ListNode head = new ListNode(0);
  18. ListNode point = head;
  19. int carry = 0;
  20. while(l1 != null && l2 != null){
  21. int sum = carry + l1.val + l2.val;
  22. point.next = new ListNode(sum%10);
  23. carry = sum/10;
  24. l1 = l1.next;
  25. l2 = l2.next;
  26. point = point.next;
  27. }
  28. while(l1 != null){
  29. int sum = carry + l1.val;
  30. point.next = new ListNode(sum%10);
  31. carry = sum/10;
  32. l1 = l1.next;
  33. point = point.next;
  34. }
  35. while(l2 != null){
  36. int sum = carry + l2.val;
  37. point.next = new ListNode(sum%10);
  38. carry = sum/10;
  39. l2 = l2.next;
  40. point = point.next;
  41. }
  42. if(carry != 0){
  43. point.next = new ListNode(carry);
  44. }
  45. return head.next;
  46. }
  47. }

 
完整运行程序:

  1. /**************************************************************
  2. * Copyright (c) 2016
  3. * All rights reserved.
  4. * 版 本 号:v1.0
  5. * 题目描述:You are given two linked lists representing two non-negative numbers.
  6. * The digits are stored in reverse order and each of their nodes contain a single digit.
  7. * Add the two numbers and return it as a linked list.
  8. * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
  9. *Output: 7 -> 0 -> 8
  10. *给定两个包含正数的链表,这个链表只能存储个位数,以链表的形式返回这个链表各位相加的和。
  11. * 输入描述:wu
  12. * 程序输出: 算法1的输出为:
  13. *0->0->10->
  14. *算法2的输出为:
  15. *8->0->0->2->
  16. * 问题分析:不能试着将链表计算的和赋给其中一个链表,并返回,应当重新建立一个新的链表,用来保存相加的和
  17. * 算法描述:这是个只能保存个位的链表,即在相加的过程中可能出现进位,例如:9->9->9链表和8这个链表相加的结果应为0->0->0->1,
  18. * 所以应当重新定义一个链表。不能将两个链表相加的和作为L1或L2链表返回,因为这要会很麻烦,
  19. * 应该重新建立一个链表,保存每个节点相加和进位的和。最后应该判断,进位标志位是否任然不为0,
  20. * 这时应该继续创建新的节点,并将进位标志作为节点的值。
  21. * 完成时间:2016-11-01
  22. ***************************************************************/
  23. package org.GuoGuoFighting.LeetCode2;
  24. class ListNode{
  25. int val;
  26. ListNode next;
  27. ListNode(){
  28. }
  29. ListNode(int x){
  30. val = x;
  31. }
  32. public String toString(){
  33. return val + "";
  34. }
  35. }
  36. class SolutionMethod1{
  37. public ListNode addTwoNumbers(ListNode l1,ListNode l2){
  38. if(l1 == null && l2 == null)
  39. return null;
  40. if(l1 == null && l2 != null)
  41. return l2;
  42. if(l1 != null && l2 == null)
  43. return l1;
  44. int tmp;
  45. ListNode root = new ListNode(0);
  46. root.next = l2;
  47. while(l1 != null && l2 != null){
  48. tmp = l1.val + l2.val;
  49. if(tmp >=10 && l1.next != null){
  50. tmp = tmp - 10;
  51. ++l1.next.val;
  52. if(l1.next.val >=10 && l1.next.next != null){
  53. l1.next.val = l1.next.val - 10;
  54. ++l1.next.next.val;
  55. }
  56. else if(l1.next.val >=10 && l1.next.next == null){
  57. l1.next.val = l1.next.val - 10;
  58. l1.next.next = new ListNode(1);
  59. }
  60. }
  61. l2.val = tmp;
  62. if(l2.next == null){
  63. l2.next = l1.next;
  64. l1 = l1.next;
  65. break;
  66. //l2.val = 0;
  67. }else{
  68. l1 = l1.next;
  69. l2 = l2.next;
  70. }
  71. }
  72. return root.next;
  73. }
  74. }
  75. class SolutionMethod2{
  76. public ListNode addTwoNumbers(ListNode l1,ListNode l2){
  77. if(l1 == null){
  78. return l2;
  79. }
  80. if(l2 == null){
  81. return l1;
  82. }
  83. ListNode head = new ListNode(0);//重新定义一个链表
  84. ListNode point = head;
  85. int carry = 0;//进位标志位
  86. while(l1 != null && l2 != null){//当两个链表都不为空时,即先计算两个链表都有节点的部分
  87. int sum = carry + l1.val + l2.val;//和为进位+l1+l2
  88. point.next = new ListNode(sum%10);//和10取余,作为返回的链表的每个节点的结果
  89. carry = sum/10;//判断家和后是否有进位标志位
  90. l1 = l1.next;
  91. l2 = l2.next;
  92. point = point.next;
  93. }
  94. while(l1 != null){//l1不为null时,表示l1后面还有节点
  95. int sum = carry + l1.val;//进位和l1节点值相加
  96. point.next = new ListNode(sum%10);
  97. carry = sum/10;
  98. l1 = l1.next;
  99. point = point.next;
  100. }
  101. while(l2 != null){
  102. int sum = carry + l2.val;
  103. point.next = new ListNode(sum%10);
  104. carry = sum/10;
  105. l2 = l2.next;
  106. point = point.next;
  107. }
  108. if(carry != 0){
  109. point.next = new ListNode(carry);//如果两个链表相加后,还有进位,则重新建立一个节点,并将point指向该节点
  110. }
  111. return head.next;//返回head.next,即point链表
  112. }
  113. }
  114. public class AddTwoNumbers {
  115. public static void main(String[] args){
  116. ListNode list11 = new ListNode(8);
  117. ListNode list12 = new ListNode(9);
  118. ListNode list13 = new ListNode(9);
  119. ListNode list21 = new ListNode(2);
  120. list11.next = list12;
  121. list12.next = list13;
  122. list13.next = null;
  123. list21.next = null;
  124. //ListNode list11 = new ListNode(9);
  125. //ListNode list12 = new ListNode(9);
  126. ////ListNode list13 = new ListNode(3);
  127. ////ListNode list14 = new ListNode(1);
  128. ////ListNode list15 = new ListNode(5);
  129. //
  130. ////ListNode list11 = null;
  131. //ListNode list21 = new ListNode(9);
  132. ////ListNode list22 = new ListNode(6);
  133. ////ListNode list23 = new ListNode(6);
  134. //
  135. ////ListNode list24 = new ListNode(6);
  136. ////ListNode list25 = new ListNode(4);
  137. ////ListNode list21 = null;
  138. //
  139. ////list11.next = null;
  140. //list11.next = list12;
  141. //list12.next = null;
  142. ////list12.next = list13;
  143. ////list13.next = list14;
  144. ////list14.next = list15;
  145. ////list15.next = null;
  146. ////list13.next = null;
  147. //
  148. //list21.next = null;
  149. ////list21.next = list22;
  150. ////list22.next = list23;
  151. ////list23.next = list24;
  152. ////list24.next = list25;
  153. ////list25.next = null;
  154. ////list23.next = null;
  155. System.out.println("算法1的输出为:");
  156. SolutionMethod1 solution1 = new SolutionMethod1();
  157. ListNode result1 = solution1.addTwoNumbers(list11, list21);
  158. SolutionMethod2 solution2 = new SolutionMethod2();
  159. ListNode result2 = solution2.addTwoNumbers(list11, list21);
  160. while(result1 != null){//打印输出链表结果的方法
  161. System.out.print(result1 + "->");
  162. result1 = result1.next;
  163. }
  164. System.out.println();
  165. System.out.println("算法2的输出为:");
  166. while(result2 != null){//打印输出链表结果的方法
  167. System.out.print(result2 + "->");
  168. result2 = result2.next;
  169. }
  170. }
  171. }


程序运行结果:
 

0 0
原创粉丝点击