• 2、LeetCode--Add Two Numbers


    question: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

    中文描述:给你两个链表代表两个非负的数字。每个数字存储在倒转顺序的列表中,并且每个节点包含一个十进制的数,将这两个数相加并返回该格式的链表。

    example的意思: 342 + 465 = 807

    分析:由于第一个节点代表第一位,我们可以采用进位相加的方法。

        有3种可能出现的情况。

        1、两个链表一样长,这时我们要注意最高为位产生进位,如果产生进位,则要在相加的结果上增加一个为1的node节点。

        2、第一个链表长度大于第二个链表,将第一个链表超出的部分增加到sum链表中,注意与第二个链表最高位相加产生的进位。

        3、第二个链表长度大于第一个链表,与情况二类似,不再赘述。

    LeetCode代码如下:

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
        int carry = 0;                    // 进位
        struct ListNode *p1, *p2;
        struct ListNode *p, *q;
        p = q = NULL;
        p1 = l1; p2 = l2;
        
        int current;
        int flag = 1;
        struct ListNode *node;
        while(p1 && p2)                   // 同步遍历链表
        {
            current = p1->val + p2->val;
            current += carry;             // 加上进位
            if(current >= 10)             // 产生进位
            {
                current -= 10;
                carry = 1;
            }
            else
                carry = 0;
            
            node = (struct ListNode *)malloc(sizeof(struct ListNode));
            node->val = current;
            node->next = NULL;
            if(flag)                       // 第一次添加节点
            {
                p = node;
                q = node;
                flag = 0;
            }
            else
            {
                q->next = node;
                q = node;
            }
               
            p1 = p1->next;
            p2 = p2->next;
        }
        
        if(p1)                            // 链表1长度大于链表2
        {
            while(p1)
            {
                current = p1->val + carry;
                if(current >= 10)
                {
                    current -= 10;
                    carry = 1;
                }
                else
                    carry = 0;
                    
                node = (struct ListNode *)malloc(sizeof(struct ListNode));
                node->val = current;
                node->next = NULL;
                q->next = node;
                q = node;
                
                p1 = p1->next; 
            }
        }
        
        if(p2)							// 链表2长度大于链表1
        {
            while(p2)
            {
                current = p2->val + carry;
                if(current >= 10)
                {
                    current -= 10;
                    carry = 1;
                }
                else
                    carry = 0;
                    
                node = (struct ListNode *)malloc(sizeof(struct ListNode));
                node->val = current;
                node->next = NULL;
                q->next = node;
                q = node;
                
                p2 = p2->next;
            }
        }
    
        if(carry == 1)                           // 链表1长度等于链表2,并且最高位相加产生进位
        {
            node = (struct ListNode *)malloc(sizeof(struct ListNode));
            node->val = 1;
            node->next = NULL;
            q->next = node;
            q = node;
        }
        
        return p;
    }
    

    如果有更好的方法,希望不吝赐教!

  • 相关阅读:
    几个常见移动平台浏览器的User-Agent
    正则表达式那些事儿(三)
    正则表达式那些事儿(二)
    正则表达式那些事儿(一)
    jQuery官网plugins栏目下那些不错的插件
    UVA 11729 Commando War 突击战 【贪心】
    HDOJ 2084 数塔 【dp】
    HDOJ 1465 不容易系列之一 【错排公式 递推】
    HDOJ 2046 骨牌铺方格 【递推】
    HDOJ 2044 一只小蜜蜂... 【递推】
  • 原文地址:https://www.cnblogs.com/wkx12/p/5457121.html
Copyright © 2020-2023  润新知