• LeetCode Add Two Numbers


    Problem:Add Two Numbers

    My Answer(C)

    struct ListNode* IniNode(int val)
    {
        struct ListNode* l = (struct ListNode*)malloc(sizeof(struct ListNode));
        
        l -> val = val;
        
        return l;
    }
    
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
    {
        int carry = 0;
        
        struct ListNode* newlist = IniNode(0);
        
        struct ListNode* p = newlist;
        struct ListNode* newp;
        
        struct ListNode* p1 = l1;
        struct ListNode* p2 = l2;
        
        int flag = 0;
        
        while (true)
        {
            // BreakJudging
            
            if (p1 == NULL && p2 == NULL) {
                flag = 3; break;
            }
            
            else if (p1 == NULL) {
                flag = 2; break;
            }
            
            else if (p2 == NULL) {
                flag = 1; break;
            }
            
            // New node, calculate its val and carry value.
            
            int newval = (p1->val+p2->val+carry) % 10;
            
            newp = IniNode(newval);
            
            carry = (p1->val+p2->val+carry) / 10;
            
            // connect p and newp, then move p to newp's pla.
            
            p -> next = newp;
            
            p = newp;
            
            // move the p1 and p2
            
            p1 = p1 -> next;
            
            p2 = p2 -> next;
            
        }
        
        // LengthJudging
        
        if (flag == 1)          // l1's length > l2's length
        {
            while (p1 != NULL)
            {
                int newval = (p1->val+carry) % 10;
                
                newp = IniNode(newval);
                
                carry = (p1->val+carry) / 10;
                
                p->next = newp;
                
                p = newp;
                
                p1 = p1->next;
            }
        }
        
        else if (flag == 2)     // l1's length < l2's length
        {
            while (p2 != NULL)
            {
                int newval = (p2->val+carry) % 10;
                
                newp = IniNode(newval);
                
                carry = (p2->val+carry) / 10;
                
                p->next = newp;
                
                p = newp;
                
                p2 = p2->next;
            }
        }
        
        if (carry != 0)         // exceeded
        {
            newp = IniNode(1);
                
            p->next = newp;
                
            p = newp;
        }
            
        p->next = NULL;
        
        return newlist->next;
    }
    
    

    Test Environment

    //
    //  main.cpp
    //  Add_Two_Numbers
    //
    //  Created by wasdns on 17/1/30.
    //  Copyright © 2017年 wasdns. All rights reserved.
    //
    
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <stdlib.h>
    
    struct ListNode {
        int val;
        struct ListNode *next;
    };
    
    struct ListNode* IniNode(int val)
    {···}
    
    struct ListNode* CreatList(int len)
    {
        struct ListNode* head = IniNode(0);
        
        struct ListNode* p = head;
        
        for (int i = 1; i <= len; i++)
        {
            struct ListNode* p1 = IniNode(0);
            
            p -> next = p1;
            
            p = p1;
            
            scanf("%d", &p->val);
        }
        
        p -> next = NULL;
        
        return head->next;
    }
    
    
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
    {···}
    
    void PrintList(struct ListNode* l)
    {
        struct ListNode* p = IniNode(0);
        
        p = l;
        
        while (p != NULL)
        {
            printf("%d ", p->val);
            
            p = p->next;
        }
        
        printf("
    ");
    }
    
    int main()
    {
        int len1, len2;
        
        scanf("%d%d", &len1, &len2);
        
        struct ListNode* l1;
        struct ListNode* l2;
        
        l1 = CreatList(len1);
        PrintList(l1);
        
        l2 = CreatList(len2);
        PrintList(l2);
        
        struct ListNode* l;
        
        l = addTwoNumbers(l1, l2);
        PrintList(l);
        
        return 0;
    }
    
    

    Better Solutions

    Evaluating Details

    建议大家参考Better Solutions中的解法,代码简洁又高效。

    2017/1/30

  • 相关阅读:
    无序数组求第K大/第K小的数
    [洛谷][二分搜索]进击的奶牛
    [015]向下类型转换和向上类型转换
    [014]析构函数为虚函数的注意事项
    [013]函数重载--int*和void*的匹配优先级
    [012]链表笔记--在链表中插入一个节点
    [011]链表笔记--删除一个链表节点
    [002]链表笔记--编程实现一个单链表的创建/测长/打印
    [C++]对象的销毁机制
    [011]默认实参
  • 原文地址:https://www.cnblogs.com/qq952693358/p/6358257.html
Copyright © 2020-2023  润新知