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

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/

    题目:

    You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

    You may assume the two numbers do not contain any leading zero, except the number 0 itself.

    Follow up:
    What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

    Example:

    Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 8 -> 0 -> 7

    题解:

    类似Add Two Numbers.

    可看成是Add Two NumbersReverse Linked List的综合. 先reverse在逐个add, 最后把结果reverse回来.

    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 class Solution {
    10     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    11         l1 = reverse(l1);
    12         l2 = reverse(l2);
    13         
    14         ListNode dummy = new ListNode(0);
    15         ListNode cur = dummy;
    16         int carry = 0;
    17         
    18         while(l1 != null || l2!= null){
    19             if(l1 != null){
    20                 carry += l1.val;
    21                 l1 = l1.next;
    22             }
    23             
    24             if(l2 != null){
    25                 carry += l2.val;
    26                 l2 = l2.next;
    27             }
    28             
    29             cur.next = new ListNode(carry%10);
    30             carry /= 10;
    31             cur = cur.next;
    32         }
    33         
    34         if(carry != 0){
    35             cur.next = new ListNode(carry);
    36         }
    37         
    38         ListNode head = dummy.next;
    39         dummy.next = null;
    40         return reverse(head);
    41     }
    42     
    43     private ListNode reverse(ListNode head){
    44         if(head == null || head.next == null){
    45             return head;
    46         }
    47         
    48         ListNode tail = head;
    49         ListNode cur = head;
    50         ListNode pre;
    51         ListNode temp;
    52         while(tail.next != null){
    53             pre = cur;
    54             cur = tail.next;
    55             temp = cur.next;
    56             cur.next = pre;
    57             tail.next = temp;
    58         }
    59         
    60         return cur;
    61     }
    62 }

    也可以使用两个stack把list 1 和 list 2 分别压进去. 再pop出来相加放到new list的head位置.

    Time Complexity: O(n). 压stack用了O(n), pop后相加用了O(n).

    Space: O(n). stack用了O(n). result list用了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         Stack<Integer> stk1 = new Stack<Integer>();
    19         Stack<Integer> stk2 = new Stack<Integer>();
    20         while(l1 != null){
    21             stk1.push(l1.val);
    22             l1 = l1.next;
    23         }
    24         while(l2 != null){
    25             stk2.push(l2.val);
    26             l2 = l2.next;
    27         }
    28         
    29         ListNode dummy = new ListNode(0);
    30         int carry = 0;
    31         while(!stk1.isEmpty() || !stk2.isEmpty()){
    32             if(!stk1.isEmpty()){
    33                 carry += stk1.pop();
    34             }
    35             if(!stk2.isEmpty()){
    36                 carry += stk2.pop();
    37             }
    38             ListNode cur = new ListNode(carry%10);
    39             cur.next = dummy.next;
    40             dummy.next = cur;
    41             carry /= 10;
    42         }
    43         if(carry != 0){
    44             ListNode cur = new ListNode(1);
    45             cur.next = dummy.next;
    46             dummy.next = cur;
    47         }
    48         return dummy.next;
    49     }
    50 }
  • 相关阅读:
    大战设计模式【5】—— 工厂方法模式
    通过spring抽象路由数据源+MyBatis拦截器实现数据库自动读写分离
    大战设计模式【4】—— 简单工厂模式
    大战设计模式【3】—— 装饰模式
    大战设计模式【2】—— 观察者模式
    大战设计模式【1】—— 策略模式
    回顾:maven配置和常用命令整理
    idea properties文件unicode码问题
    Nginx学习笔记
    tomcat添加context方式部署web应用
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/6221191.html
Copyright © 2011-2022 走看看