C语言 | Leetcode C语言题解之第445题两数相加II

题目:

题解:

cpp 复制代码
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    int stack1[100];
    int stack2[100];
    int top1 = 0;
    int top2 = 0;
    int carry = 0;
    int sum = 0;
    struct ListNode* temp = NULL;
    struct ListNode* head = NULL;
    while (l1) {
        stack1[top1++] = l1->val;
        l1 = l1->next;
    }
    while (l2) {
        stack2[top2++] = l2->val;
        l2 = l2->next;
    }
    while (top1 || top2 || carry) {
        int m = top1 > 0 ? stack1[--top1] : 0;
        int n = top2 > 0 ? stack2[--top2] : 0;
        sum = m + n + carry;
        carry = sum / 10;
        head = malloc(sizeof(struct ListNode));
        head->val = sum % 10;
        head->next = temp;
        temp = head;
    }
    return head;
}
相关推荐
GalaxyPokemon28 分钟前
LeetCode - 704. 二分查找
数据结构·算法·leetcode
ScilogyHunter3 小时前
vscode的c工程配置文件详解
c语言·ide·vscode
徐新帅4 小时前
基于 C 语言的图书管理系统开发详解
c语言·开发语言·数据结构
liuqun03195 小时前
开心灿烂go开发面试题
算法·leetcode·golang
এ᭄画画的北北5 小时前
力扣-279.完全平方数
数据结构·算法·leetcode
GalaxyPokemon6 小时前
LeetCode - LCR 173. 点名
算法·leetcode·职场和发展
范纹杉想快点毕业6 小时前
初探Qt信号与槽机制
java·c语言·开发语言·c++·qt·visualstudio·visual studio
C++ 老炮儿的技术栈8 小时前
visual studio 2022更改主题为深色
c语言·开发语言·c++·ide·windows·git·visual studio
CPETW10 小时前
同旺科技 USB TO SPI / I2C适配器(专业版)--EEPROM读写——C
c语言·开发语言·科技·stm32·单片机·嵌入式硬件·电子
呆呆的小鳄鱼10 小时前
IO之详解cin(c++IO关键理解)
linux·c语言·c++