leetcode 002 add two numbers C语言

问题描述

leetcode 002 add two numbers C语言
 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    struct ListNode *l, *p;
    l = (struct ListNode*)malloc(sizeof(struct ListNode));
    l->val = l1->val + l2->val;
    p = l;
    l1 = l1->next;
    l2 = l2->next;
    while(l1||l2||p->val>9)
    {
        p->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->next->val = p->val/10;
        p->val %= 10;
        p = p->next;

        if(l1)
        {
            p->val += l1->val;
            l1 = l1->next;
        }
         if(l2)
        {
            p->val += l2->val;
            l2 = l2->next;
        }
    }
    return l;
}

请问这样有错吗?我自己反复看了觉得没有问题啊,原题 如下
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

解决方案

链表没有终结.每次malloc后马上给新生出的节点的next赋NULL值就可以了.移动两个地方。

l->next = NULL;

p->next->next = NULL;
时间: 2024-08-03 07:43:11

leetcode 002 add two numbers C语言的相关文章

LeetCode 2 Add Two Numbers

翻译: 给你两个表示两个非负数字的链表.数字以相反的顺序存储,其节点包含单个数字.将这两个数字相加并将其作为一个链表返回. 输入: (2 -> 4 -> 3) + (5 -> 6 -> 4) 输出: 7 -> 0 -> 8 原题: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of

求帮助:2. Add Two Numbers ,已疯,谢谢

问题描述 求帮助:2. Add Two Numbers ,已疯,谢谢 自己运行: 提交以后: 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { struct ListNode *l,

[LeetCode] Sum of Square Numbers 平方数之和

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5  Example 2: Input: 3 Output: False 这道题让我们求一个数是否能由平方数之和组成,刚开始博主没仔细看题,没有

[LeetCode] 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 ->

leetcode 67 Add Binary

Add Binary Total Accepted: 46815 Total Submissions: 189215 My Submissions                       Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 我的解决方案: class Solu

[LeetCode]165.Compare Version Numbers

[题目] Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . c

LeetCode 258 Add Digits(数字相加,数字根)

翻译 给定一个非负整型数字,重复相加其所有的数字直到最后的结果只有一位数. 例如: 给定sum = 38,这个过程就像是:3 + 8 = 11,1 + 1 = 2,因为2只有一位数,所以返回它. 紧接着: 你可以不用循环或递归在O(1)时间内完成它吗? 原文 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given

[LeetCode]--415. Add Strings

Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero.

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