【力扣(LeetCode)】【C/C++】【2.两数相加】

学习时间:

        2023年3月7日


题目描述:


 题解分享:

// 作     者 : 繁 华 倾 夏
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>                                             // 调用malloc函数

//Definition for singly-linked list.                            // 单向链表的定义
struct ListNode {
    int val;
    struct ListNode* next;
};

// l1:链表  l2:链表
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode* head = NULL, * tail = NULL;                // 定义两个指针指向头和尾
    int carry = 0;                                              // 进位值
    while (l1 || l2) {                                          // 循环遍历l1和l2,只有两个链表都不为空时才跳出循环
        int n1 = l1 ? l1->val : 0;                              // 如果n1的值不为空,则进行赋值,否则将n1的值赋为0
        int n2 = l2 ? l2->val : 0;                              // 如果n2的值不为空,则进行赋值,否则将n2的值赋为0
        int sum = n1 + n2 + carry;                              // 两个链表的各位数加上进位值判断是否需要进位
        if (!head) {                                            // 如果头结点不为空
            head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
            tail->val = sum % 10;                               // 将当前两各位数之后取余后链接到链表,确保当前位数小于10
            tail->next = NULL;                                  // 移动链表,等待下次的数值位存入
        }
        else {                                                  // 如果头结点为空,则是首次插入数据,首次插入数据则赋值与头结点的下一位
            tail->next = (struct ListNode*)malloc(sizeof(struct ListNode));
            tail->next->val = sum % 10;                         // 将数值位存入数据(头结点未存入数据)
            tail = tail->next;                                  // 移动链表的尾指针
            tail->next = NULL;                                  // 尾指针赋空
        }
        carry = sum / 10;                                       // 判断当前是否需要进位
        if (l1) {                                               // 判断是否到达链表尾
            l1 = l1->next;                                      // 移动链表
        }
        if (l2) {                                               // 判断是否到达链表尾
            l2 = l2->next;                                      // 移动链表
        }
    }
    if (carry > 0) {                                            // 如果进位有数值
        tail->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        tail->next->val = carry;                                // 将数值存入下一结点,等待下一循环的两个链表的各位数值存入数值
        tail->next->next = NULL;                                // 将链表尾部的netx赋null
    }
    return head;                                                // 返回头结点
}



// 从尾部插入数据
void SListPushBack(struct ListNode** pphead, int x) {
    struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newnode->val = x;
    newnode->next = NULL;
    if (*pphead == NULL) {
        *pphead = newnode;
    }
    else {
        // 找到尾节点
        struct ListNode* tail = *pphead;
        while (tail->next != NULL) {
            tail = tail->next;
        }
        tail->next = newnode;
    }
}

// 打印链表
void SListPrint(struct ListNode* phead)
{
    struct ListNode* cur = phead;
    while (cur != NULL) {
        printf("%d->", cur->val);
        cur = cur->next;
    }
    printf("NULL\n");
}


// 测试用例
// 输入 l1 = [2, 4, 3], l2 = [5, 6, 4]
// 输出 [7, 0, 8]
int main() {
    struct ListNode* l1 = NULL;                  // 建立空链表
    struct ListNode* l2 = NULL;                  // 建立空链表
    SListPushBack(&l1, 2);                       // 为链表插入数据
    SListPushBack(&l1, 4);                       // 为链表插入数据
    SListPushBack(&l1, 3);                       // 为链表插入数据
    SListPushBack(&l2, 5);                       // 为链表插入数据
    SListPushBack(&l2, 6);                       // 为链表插入数据
    SListPushBack(&l2, 4);                       // 为链表插入数据

    struct ListNode* re = addTwoNumbers(l1, l2);

    SListPrint(re);                             // 打印链表
    return 0;
}

【繁华倾夏】【每日力扣题解分享】【Day52】


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值