leecode题解-2. 两数相加

模拟法思路解题,详细思路可以看leecode官方题解。

代码如下:

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode* dummy = malloc(sizeof(struct ListNode));
    struct ListNode* tail = dummy;
    int carry = 0;

    while (l1 || l2) {
        int sum = carry;

        if (l1) {
            sum += l1->val;
            l1 = l1->next;
        }

        if (l2) {
            sum += l2->val;
            l2 = l2->next;
        }

        carry = sum / 10;
        sum = sum % 10;

        struct ListNode* newNode = malloc(sizeof(struct ListNode));
        newNode->val = sum;
        newNode->next = NULL;
        tail->next = newNode;
        tail = tail->next;
    }

    if (carry > 0) {
        struct ListNode* newNode = malloc(sizeof(struct ListNode));
        newNode->val = carry;
        newNode->next = NULL;
        tail->next = newNode;
    }

    struct ListNode* result = dummy->next;
    free(dummy);
    return result;
}

包含测试用例的本地完整运行代码如下:

#include <stdio.h>
#include <stdlib.h>

struct ListNode {
    int val;
    struct ListNode* next;
};

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode* dummy = malloc(sizeof(struct ListNode));
    struct ListNode* tail = dummy;
    int carry = 0;

    while (l1 || l2) {
        int sum = carry;

        if (l1) {
            sum += l1->val;
            l1 = l1->next;
        }

        if (l2) {
            sum += l2->val;
            l2 = l2->next;
        }

        carry = sum / 10;
        sum = sum % 10;

        struct ListNode* newNode = malloc(sizeof(struct ListNode));
        newNode->val = sum;
        newNode->next = NULL;
        tail->next = newNode;
        tail = tail->next;
    }

    if (carry > 0) {
        struct ListNode* newNode = malloc(sizeof(struct ListNode));
        newNode->val = carry;
        newNode->next = NULL;
        tail->next = newNode;
    }

    struct ListNode* result = dummy->next;
    free(dummy);
    return result;
}

int main() {
    // 创建测试用例链表 l1: [4, 5, 6]
    struct ListNode* l1 = malloc(sizeof(struct ListNode));
    l1->val = 4;
    l1->next = malloc(sizeof(struct ListNode));
    l1->next->val = 5;
    l1->next->next = malloc(sizeof(struct ListNode));
    l1->next->next->val = 6;
    l1->next->next->next = NULL;

    // 创建测试用例链表 l2: [4, 5, 1]
    struct ListNode* l2 = malloc(sizeof(struct ListNode));
    l2->val = 4;
    l2->next = malloc(sizeof(struct ListNode));
    l2->next->val = 5;
    l2->next->next = malloc(sizeof(struct ListNode));
    l2->next->next->val = 1;
    l2->next->next->next = NULL;

    // 调用 addTwoNumbers 函数进行链表相加
    struct ListNode* result = addTwoNumbers(l1, l2);

    // 遍历输出结果链表的值
    printf("Result: ");
    while (result) {
        printf("%d ", result->val);
        result = result->next;
    }
    printf("\n");

    // 释放链表的内存
    struct ListNode* curr = l1;
    while (curr) {
        struct ListNode* temp = curr;
        curr = curr->next;
        free(temp);
    }

    curr = l2;
    while (curr) {
        struct ListNode* temp = curr;
        curr = curr->next;
        free(temp);
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值