zoukankan      html  css  js  c++  java
  • LeetCode

    LeetCode - Add Two Numbers

    2013.12.1 21:03

    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

    Solution:

      Given two number represented as single-linked list, add them up and return the sum as a list in similar form.

      It seems easy enough, but you have to take care of different cases, mainly the following:

        1. NULL list

        2. the lengths of two lists are different

        3. 999 + 111 = 1110, the sum list needs one more digit

      Add up both 'numbers' one digit at a time, and record the carry for each digit. Make sure you write your code carefully, especially because this is linked list, the tricky thing.

      Time complexity is O(max(m, n)) where m and n are lengths of both linked lists. Space complexity is O(1) with some extra variables.

    Accepted code:

      1 // 2WA, 1RE, 1AC, not so easy..
      2 /**
      3  * Definition for singly-linked list.
      4  * struct ListNode {
      5  *     int val;
      6  *     ListNode *next;
      7  *     ListNode(int x) : val(x), next(NULL) {}
      8  * };
      9  */
     10 class Solution {
     11 public:
     12     ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
     13         // IMPORTANT: Please reset any member data you declared, as
     14         // the same Solution instance will be reused for each test case.
     15         
     16         if(l1 == nullptr || l2 == nullptr){
     17             return nullptr;
     18         }
     19         
     20         ListNode *head;
     21         ListNode *ptr, *p1, *p2;
     22         bool first;
     23         ListNode *par1, *par2, *par;
     24         ListNode *r1, *r2, *rr;
     25         int carry;
     26         
     27         p1 = l1;
     28         p2 = l2;
     29         
     30         carry = 0;
     31         
     32         r1 = par1 = new ListNode(0);
     33         r2 = par2 = new ListNode(0);
     34         rr = par = new ListNode(0);
     35         
     36         par1->next = l1;
     37         par2->next = l2;
     38         par->next = nullptr;
     39         
     40         first = true;
     41         //while(p1 != nullptr && p2 != nullptr){
     42         // 1WA here, {1,8}, {0}
     43         while(p1 != nullptr || p2 != nullptr){
     44             ptr = new ListNode(0);
     45             if(p1 != nullptr){
     46                 ptr->val += p1->val;
     47             }
     48             if(p2 != nullptr){
     49                 ptr->val += p2->val;
     50             }
     51             /*
     52             1WA here, wrong if statement
     53             if(p1 != nullptr){
     54                 ptr->val += carry;
     55             }
     56             */
     57             ptr->val += carry;
     58 
     59             par->next = ptr;
     60             par = par->next;
     61             if(p1 != nullptr){
     62                 par1 = par1->next;
     63             }
     64             if(p2 != nullptr){
     65                 par2 = par2->next;
     66             }
     67 
     68             if(first){
     69                 head = ptr;
     70                 first = false;
     71             }
     72             
     73             if(ptr->val >= 10){
     74                 ptr->val -= 10;
     75                 carry = 1;
     76             }else{
     77                 carry = 0;
     78             }
     79             
     80             if(p1 != nullptr){
     81                 p1 = p1->next;
     82             }
     83             if(p2 != nullptr){
     84                 p2 = p2->next;
     85             }
     86             ptr = ptr->next;
     87         }
     88         if(carry){
     89             ptr = new ListNode(1);
     90             par->next = ptr;
     91         }
     92         
     93         // 1RE here, parent pointer moved, must have an extra pointer to record the parent node on the front
     94         delete r1;
     95         delete r2;
     96         delete rr;
     97         
     98         return head;
     99     }
    100 };
  • 相关阅读:
    Ubuntu 18.04 LTS 启用BBR
    MySQL with JDBC <一>
    HTML 实录 <一>
    Nginx CloudFlare 客户端真实IP
    Ubuntu 16.04 释放升级到 18.04 后, man: command exited with status 4
    JSP 从入门到精通 <一>
    Nginx URL重写
    JavaScript 修改 CSS 伪类属性
    i-83.net quadhost子产品
    HTML 重定向 页面跳转
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3452997.html
Copyright © 2011-2022 走看看