zoukankan      html  css  js  c++  java
  • 167. Add Two Numbers【easy】

    You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

     
    Example

    Given 7->1->6 + 5->9->2. That is, 617 + 295.

    Return 2->1->9. That is 912.

    Given 3->1->5 and 5->9->2, return 8->0->8.

    题意

    你有两个用链表代表的整数,其中每个节点包含一个数字。数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头。写出一个函数将两个整数相加,用链表形式返回和。

    解法一:

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) {
     7  *         val = x;
     8  *         next = null;
     9  *     }
    10  * }
    11  */
    12 public class Solution {
    13     public ListNode addLists(ListNode l1, ListNode l2) {
    14         if(l1 == null && l2 == null) {
    15             return null;
    16         }
    17             
    18         ListNode head = new ListNode(0);
    19         ListNode point = head;
    20         int carry = 0;
    21         while(l1 != null && l2!=null){
    22             int sum = carry + l1.val + l2.val;
    23             point.next = new ListNode(sum % 10);
    24             carry = sum / 10;
    25             l1 = l1.next;
    26             l2 = l2.next;
    27             point = point.next;
    28         }
    29         
    30         while(l1 != null) {
    31             int sum =  carry + l1.val;
    32             point.next = new ListNode(sum % 10);
    33             carry = sum /10;
    34             l1 = l1.next;
    35             point = point.next;
    36         }
    37         
    38         while(l2 != null) {
    39             int sum =  carry + l2.val;
    40             point.next = new ListNode(sum % 10);
    41             carry = sum /10;
    42             l2 = l2.next;
    43             point = point.next;
    44         }
    45         
    46         if (carry != 0) {
    47             point.next = new ListNode(carry);
    48         }
    49         return head.next;
    50     }
    51 }

    中规中矩的解法

    解法二:

     1 public class Solution {
     2     /**
     3      * @param l1: the first list
     4      * @param l2: the second list
     5      * @return: the sum list of l1 and l2
     6      */
     7     public ListNode addLists(ListNode l1, ListNode l2) {
     8         ListNode dummy = new ListNode(0);
     9         ListNode tail = dummy;
    10 
    11         int carry = 0;
    12         for (ListNode i = l1, j = l2; i != null || j != null; ) {
    13             int sum = carry;
    14             sum += (i != null) ? i.val : 0;
    15             sum += (j != null) ? j.val : 0;
    16 
    17             tail.next = new ListNode(sum % 10);
    18             tail = tail.next;
    19 
    20             carry = sum / 10;
    21             i = (i == null) ? i : i.next;
    22             j = (j == null) ? j : j.next;
    23         }
    24 
    25         if (carry != 0) {
    26             tail.next = new ListNode(carry);
    27         }
    28         return dummy.next;
    29     }
    30 }

    比较简明的写法,且使用了dummy节点,参考@NineChapter 的代码

    解法三:

     1 public class Solution {
     2     /*
     3      * @param l1: the first list
     4      * @param l2: the second list
     5      * @return: the sum list of l1 and l2 
     6      */
     7     public ListNode addLists(ListNode l1, ListNode l2) {      
     8         ListNode root = new ListNode(-1);
     9         ListNode result = root;
    10         int carry = 0;        
    11         
    12         while( l1 != null || l2 != null || carry == 1){
    13             int value = 0;
    14             if(l1 != null){
    15                 value += l1.val;
    16                 l1 = l1.next;
    17             }
    18             if( l2 != null){
    19                 value += l2.val;
    20                 l2 = l2.next;
    21             }
    22             
    23             value += carry;            
    24             root.next = new ListNode(value % 10);
    25             carry = value / 10;
    26             
    27             root = root.next;
    28       
    29         }
    30         
    31         return result.next;
    32     }
    33 }

    解法四:

     1 class Solution {
     2 public:
     3     ListNode* addLists(ListNode* l1, ListNode* l2) {
     4         ListNode *head = new ListNode(0);
     5         ListNode *ptr = head;
     6         int carry = 0;
     7         while (true) {
     8             if (l1 != NULL) {
     9                 carry += l1->val;
    10                 l1 = l1->next;
    11             }
    12             if (l2 != NULL) {
    13                 carry += l2->val;
    14                 l2 = l2->next;
    15             }
    16             ptr->val = carry % 10;
    17             carry /= 10;
    18             // 当两个表非空或者仍有进位时需要继续运算,否则退出循环
    19             if (l1 != NULL || l2 != NULL || carry != 0) {
    20                 ptr = (ptr->next = new ListNode(0));
    21             } else {
    22                 break;
    23             }
    24         }
    25         return head;
    26     }
    27 };

    非常简明的代码,参考@NineChapter 的代码

  • 相关阅读:
    docker配置国内加速器
    pwm原理与stm32控制
    【C语言】赋值表达式自增自减逗号表达式
    linux下利用paho.mqtt.c进行mqtt开发 #c编程
    linux下tcp #c编程
    windows下的linux子系统虚拟串口的使用
    linux下搭建mqtt服务器(mosquitto)
    linux下paho.mqtt.c下载安装
    树莓派&mysql数据库
    vscode+mysql数据库编程
  • 原文地址:https://www.cnblogs.com/abc-begin/p/8151199.html
Copyright © 2011-2022 走看看