[LeetCode]2.Add Two Numbers

来源:互联网 发布:手机淘宝如何找旗舰店 编辑:程序博客网 时间:2024/06/10 07:25

题目:

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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Subscribe to see which companies asked this question.

英语不好,题目都没有读懂,刚开始以为给的数字是按照从左到右是从高位到低位的顺序,后来才知道,现在在这里翻译一边题目。

给你两个非空单链结构的列表,列表的每个元素包含一个非负整数,数的高低位已经倒置(从低位到高位)排列,每个节点只包含一位,将这两个列表相加,返回结果列表。

假设两个列表所给的最高位不会时0,除了列表是自然数0外。


这里可以提取模型,遍历两个list的模型,之前自己的思路一直都是,三个循环:第一个循环直到至少其中一个list为空,第二个循环到第一个list为空,第三个循环到第二个list为空(第二个第三个循环不一定会进入)。所以按照自己的思路,写了一下代码(C语言):

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {    struct ListNode *h1, *h2, *p, *head;    int flag = 0;    h1 = l1;    h2 = l2;    head = NULL;    while (h1 != NULL && h2 != NULL) {        if (head) {            p->next = (struct ListNode*)malloc(sizeof(struct ListNode));            p = p->next;            p->val = (h1->val + h2->val + flag) % 10;            flag = (h1->val + h2->val + flag) / 10;            p->next = NULL;            h1 = h1->next;            h2 = h2->next;        }        else {            head = (struct ListNode*)malloc(sizeof(struct ListNode));            head->val = (h1->val + h2->val + flag) % 10;            head->next = NULL;            flag = (h1->val + h2->val + flag) / 10;            p = head;            h1 = h1->next;            h2 = h2->next;        }    }        while (h1 != NULL) {        p->next = (struct ListNode*)malloc(sizeof(struct ListNode));        p = p->next;        p->val = (h1->val + flag) % 10;        p->next = NULL;        flag = (h1->val + flag) / 10;        h1 = h1->next;    }            while (h2 != NULL) {        p->next = (struct ListNode*)malloc(sizeof(struct ListNode));        p = p->next;        p->val = flag ? (h2->val + 1) % 10 : h2->val;        p->next = NULL;        flag = (h2->val + flag) / 10;        h2 = h2->next;    }        if (flag) {        p->next = (struct ListNode*)malloc(sizeof(struct ListNode));        p = p->next;        p->val = 1;        p->next = NULL;    }    return head;}

(自己的代码总是要debug好长时间,小毛病不断,如果不是提交我自己都不知道哪里错了,自己也很愁)

这里总结一下调试出来的bug:

1. 节点的next没有注意造成的死循环,对策,以后凡是链的,先不求值,先写next

2.flag一度认为和进位没有关系,我智障了,小学的知识。

3.忘加最后的进位1(99+1)


看过大神的代码,觉得好简介,也学到一种遍历两个list的模型方法,一个循环,直到两个都为空,循环内部先判断是否为空,再进行相应的操作,代码:

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {    struct ListNode *h, *p, *c1, *c2;    int num = 0;    c1 = l1;    c2 = l2;    h = p = NULL;    while (c1 || c2) {        num = num / 10;        if (c1) {            num += c1->val;            c1 = c1->next;        }        if (c2) {            num += c2->val;            c2 = c2->next;        }        if (h == NULL) {            h = (struct ListNode*)malloc(sizeof(struct ListNode));            h->next = NULL;            h->val = num % 10;            p = h;        }        else {            p->next = (struct ListNode*)malloc(sizeof(struct ListNode));            p = p->next;            p->val = num % 10;            p->next = NULL;        }    }    if (num/10) {        p->next = (struct ListNode*)malloc(sizeof(struct ListNode));        p = p->next;        p->val = 1;        p->next = NULL;    }    return h;}


0 0
原创粉丝点击