博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode Add Two Numbers
阅读量:7103 次
发布时间:2019-06-28

本文共 3481 字,大约阅读时间需要 11 分钟。

Problem:

885822-20170130183310745-1618160310.png

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 
#include
#include
#include
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("\n");}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;}

Evaluating Details

885822-20170130183842917-1102558928.png

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

2017/1/30

转载地址:http://rzchl.baihongyu.com/

你可能感兴趣的文章
SpringMVC (四)MultiActionController
查看>>
unity_粒子系统
查看>>
初识linux内核漏洞利用
查看>>
HDU - 6393 Traffic Network in Numazu(树链剖分+基环树)
查看>>
HDU 3826 Squarefree number
查看>>
python数据分析实战---基础准备
查看>>
elasticsearch内存优化设置
查看>>
从键盘上连续录入一批整数,比较并输出其中的最大值和最小值,当输入数字0时结束循环...
查看>>
mysql不同端口的连接
查看>>
递归 正则表达式 杨辉三角
查看>>
转载 - sql分页优化
查看>>
比较好的Dapper封装的仓储实现类 来源:https://www.cnblogs.com/liuchang/articles/4220671.html...
查看>>
2018焦作区域赛E. Resistors in Parallel
查看>>
WebService之Axis2(2):复合类型数据的传递
查看>>
第十一次作业
查看>>
CSVN部署安装,实现web管理svn
查看>>
Methods of Applied Mathematics
查看>>
ie10的input框新特性的去除
查看>>
IOS证书/私钥/代码签名/描述文件
查看>>
二分图的匹配的基础知识
查看>>