zoukankan      html  css  js  c++  java
  • LeetCode 2. Add Two Numbers

    原题链接:https://leetcode.com/problems/add-two-numbers/

    题目:

    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

    题解:

    思路两个val和进位carry相加,组成新点往后连,注意两个list长度不同和最后是否还有一个进位的情况。

    Time Complexity: O(n), n是较长list的长度.

    Space: O(n).

    AC Java:

     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         
    18         ListNode dummy = new ListNode(0);
    19         ListNode cur = dummy;
    20         int carry = 0;
    21         
    22         while(l1 != null || l2 != null){
    23             if(l1 != null){
    24                 carry += l1.val;
    25                 l1 = l1.next;
    26             }
    27             if(l2 != null){
    28                 carry += l2.val;
    29                 l2 = l2.next;
    30             }
    31             
    32             cur.next = new ListNode(carry%10);
    33             cur = cur.next;
    34             carry = carry/10;
    35         }
    36         if(carry != 0){
    37             cur.next = new ListNode(carry);
    38         }
    39         return dummy.next;
    40     }
    41 }

    也可以选择更改较长list的node val.

    不需要n的extra space.

    Time Complexity: O(n). 

    Space: O(1).

    AC Java:

     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         int len1 = 0;
    18         ListNode cur = l1;
    19         while(cur != null){
    20             cur = cur.next;
    21             len1++;
    22         }
    23         
    24         int len2 = 0;
    25         cur = l2;
    26         while(cur != null){
    27             cur = cur.next;
    28             len2++;
    29         }
    30         
    31         ListNode dummy = new ListNode(0);
    32         if(len1 > len2){
    33             dummy.next = l1;
    34         }else{
    35             dummy.next = l2;
    36         }
    37         cur = dummy;
    38         int carry = 0;
    39         
    40         while(l1 != null || l2 != null){
    41             if(l1 != null){
    42                 carry += l1.val;
    43                 l1 = l1.next;
    44             }
    45             if(l2 != null){
    46                 carry += l2.val;
    47                 l2 = l2.next;
    48             }
    49             cur.next.val = carry%10;
    50             cur = cur.next;
    51             carry /= 10;
    52         }
    53         if(carry != 0){
    54             cur.next = new ListNode(1);
    55         }
    56         return dummy.next;
    57     }
    58 }

    跟上Add Two Numbers IISum of Two Integers.

  • 相关阅读:
    Navicat远程连接服务器Mysql
    JSP与Servlet之间传值
    JSP获取绝对路径
    PIL的库学习
    科学计算与可视化
    预测球类比赛结果
    预测球类比赛结果
    汉诺塔问题
    有进度条圆周率计算
    turtle学习心得
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4825009.html
Copyright © 2011-2022 走看看