zoukankan      html  css  js  c++  java
  • leetcode 2 Add Two Numbers(链表)

    题目:

    You are given two non-empty linked lists representing two non-negative integers. 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.

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

    Example

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8
    Explanation: 342 + 465 = 807.

    思路:

    题目主要考察链表,利用链表来模拟实现加法,容易出错误的地方在于对加法进位的处理。我自己的思路比较复杂,主要是考虑当l1,l2其中有一个为null时,将进位1转移到l1或者l2,转移后如果仍有一个为Null,则可直接退出循环。

    代码:

     1 struct ListNode {
     2   int val;
     3   ListNode *next;
     4   ListNode(int x) : val(x), next(NULL) {}
     5 };
     6 
     7 class Solution {
     8 public:
     9     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    10         int left = 0;
    11         ListNode* first = new struct ListNode(0);
    12         ListNode* now = first;
    13         while(l1 != NULL && l2 != NULL){
    14             int temp = l1->val + l2->val + left;
    15             now->val = temp % 10;
    16             if(temp >= 10)
    17                 left = 1;
    18             else
    19                 left = 0;
    20             l1 = l1->next;
    21             l2 = l2->next;
    22             if(l1 == NULL && left == 1){
    23                 ListNode* newNode = new struct ListNode(1);
    24                 l1 = newNode;
    25                 left = 0;
    26             }
    27             else if(l2 == NULL && left == 1){
    28                 ListNode* newNode = new struct ListNode(1);
    29                 l2 = newNode;
    30                 left = 0;
    31             }
    32             if(l1 != NULL && l2 != NULL)
    33             {
    34                 ListNode* nextNode = new struct ListNode(0);
    35                 now->next = nextNode;
    36                 now = now->next;
    37             }
    38         }
    39         if(l1 == NULL)
    40             now->next = l2;
    41         else
    42             now->next = l1;
    43         return first;
    44     }
    45 };

    网上的一种思路是将三者同等看待,只要l1,l2其中有一个不为null,就可以接着计算。

    代码比较简洁,也贴一下:

     1 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
     2     ListNode dummyHead = new ListNode(0);
     3     ListNode p = l1, q = l2, curr = dummyHead;
     4     int carry = 0;
     5     while (p != null || q != null) {
     6         int x = (p != null) ? p.val : 0;
     7         int y = (q != null) ? q.val : 0;
     8         int sum = carry + x + y;
     9         carry = sum / 10;
    10         curr.next = new ListNode(sum % 10);
    11         curr = curr.next;
    12         if (p != null) p = p.next;
    13         if (q != null) q = q.next;
    14     }
    15     if (carry > 0) {
    16         curr.next = new ListNode(carry);
    17     }
    18     return dummyHead.next;
    19 }

    复杂度分析:

    讲道理,我的复杂度是min(l1,l2),网上思路的复杂度是max(l1,l2)。不过都是线性的,基本也没啥区别吧。

    
    
  • 相关阅读:
    Promise
    location(未完)
    Web Storage API : LocalStroage
    从vmware下载到Linux环境下jdk和maven的安装
    2015.8.1 bootstrap学习(个人每日学习的随笔,比较凌乱
    git报错 error: cannot stat ‘'web/js': Permission denied
    移动端的自适应
    Socket .net MVC 的配置 包括异常断开
    ExtJS ComboBox之 键入自动查询
    ExtJS ComboBox之 2级联动
  • 原文地址:https://www.cnblogs.com/tracy520/p/8320795.html
Copyright © 2011-2022 走看看