zoukankan      html  css  js  c++  java
  • [LeetCode] Add Two Numbers

    https://oj.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

    Solution:

    Keep track of the carry(进位) using a variable and simulate digits-by-digits sum from the head of list, which contains the least-significant digit(最低位).

    Take extra caution of the following cases:

    1. When one list is longer than the other.

    2. The sum could have an extra carry of one at the end, which is easy to forget.(e.g., (9->9) + (1) = (0->0->1))

    Code:

    /**
     * Author : Acjx
     * Email  : zhoujx0219@163.com
     */
    
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution 
    {
    public:
        ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) 
        {
            ListNode *dummyHead = new ListNode(0);
            
            ListNode *p = l1;
            ListNode *q = l2;
            ListNode *cur = dummyHead;
            
            // Present carry(进位)
            int carry = 0;
            
            // Process two lists and notice that one list may be longer than the other.
            while (p != NULL || q != NULL)
            {
                int x = (p != NULL) ? p->val : 0;
                int y = (q != NULL) ? q->val : 0;
                int digit = carry + x + y;
                
                carry = digit / 10;
                cur->next = new ListNode(digit % 10);
                cur = cur->next;
                
                if (p != NULL) p = p->next;
                if (q != NULL) q = q->next;
            }
            
            // The sum could have an extra carry of one at the end, which is easy to forget
            if (carry > 0)
            {
                cur->next = new ListNode(carry);
            }
            
            return dummyHead->next;
        }
    };

    Tips:

    使用了 dummyHead,简化了代码。如果不使用 dummyHead,尾插需要判断头指针是否为空,示例代码如下:

        ListNode *list = NULL;
        ListNode *tail = NULL;
        ...
        if (list == NULL)
        {
            // 记录新的链表头
            list = new ListNode(sum);
            tail = list;
        }
        else
        {
            tail->next = new ListNode(sum);
            tail = tail->next;
        }
        ...
        return list;
     
    显然,过于繁琐。
     
    Using Dummy Node

    Scenario: When the head is not determinated.

  • 相关阅读:
    FMDB 使用方法
    Masonry
    iOS请求服务器数据去空NSNull
    NSProgress
    根据图标名称动态设置TreeList图标
    控制显示行头列(Indicator)
    窗体内元素遍历-通用方法(DevExpress 中BarManager的遍历)
    BarManager相关使用
    C# 枚举变量
    dev TreeList 获取可视区域节点方法
  • 原文地址:https://www.cnblogs.com/jianxinzhou/p/4312060.html
Copyright © 2011-2022 走看看