Leetcode—445.两数相加II【中等】

2023每日刷题(六十七)

Leetcode—445.两数相加II

在这里插入图片描述

实现代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
   
    struct ListNode* head1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    head1->next = NULL;
    struct ListNode* head2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    head2->next = NULL;
    struct ListNode* p, *q, *p2, *q2;
    p = l1;
    p2 = l1->next;
    q = l2;
    q2 = l2->next;
    // 链表翻转
    while(p != NULL) {
   
        p->next = head1->next;
        head1->next = p;
        p = p2;
        if(p2 == NULL) {
   
            break;
        }
        p2 = p2->next;
        
    }
    while(q != NULL) {
   
        q->next = head2->next;
        head2->next = q;
        q = q2;
        if(q2 == NULL) {
   
            break;
        }
        q2 = q2->next;
        
    }
    p = head1->next;
    q = head2->next;
    head1->next = NULL;
    int c = 0;
    while(p && q) {
   
        struct ListNode* s = (struct ListNode*)malloc(sizeof(struct ListNode));
        s->next = NULL;
        int sum = p->val + q->val + c;
        c = sum / 10;
        s->val = sum % 10;
        s->next = head1->next;
        head1->next = s;
        p = p->next;
        q = q->next;
    }
    while(p != NULL) {
   
        struct ListNode* s = (struct ListNode*)malloc(sizeof(struct ListNode));
        s->next = NULL;
        int sum = p->val + c;
        c = sum / 10;
        s->val = sum % 10;
        s->next = head1->next;
        head1->next = s;
        p = p->next;
    }
    while(q != NULL) {
   
        struct ListNode* s = (struct ListNode*)malloc(sizeof(struct ListNode));
        s->next = NULL;
        int sum = q->val + c;
        c = sum / 10;
        s->val = sum % 10;
        s->next = head1->next;
        head1->next = s;
        q = q->next;
    }
    if(c) {
   
        struct ListNode* s = (struct ListNode*)malloc(sizeof(struct ListNode));
        s->next = NULL;
        s->val = c;
        s->next = head1->next;
        head1->next = s;
    }
    return head1->next;
}

运行结果

在这里插入图片描述
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

相关推荐

  1. LeetCode 445.相加||

    2023-12-24 05:24:01       22 阅读
  2. Leetcode454. 四相加 II

    2023-12-24 05:24:01       52 阅读
  3. LeetCode454. 四相加 II

    2023-12-24 05:24:01       36 阅读
  4. 454. 四相加 II

    2023-12-24 05:24:01       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-24 05:24:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-24 05:24:01       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-24 05:24:01       82 阅读
  4. Python语言-面向对象

    2023-12-24 05:24:01       91 阅读

热门阅读

  1. kotlin ——数组

    2023-12-24 05:24:01       61 阅读
  2. IO内存访问函数

    2023-12-24 05:24:01       65 阅读
  3. Linux rm 命令

    2023-12-24 05:24:01       57 阅读
  4. 2023.11.24 信息学日志

    2023-12-24 05:24:01       60 阅读
  5. mybatis-plus阻止全表更新与删除

    2023-12-24 05:24:01       70 阅读
  6. MySQL中EXPLAIN关键字解释

    2023-12-24 05:24:01       52 阅读
  7. MySQL常用语句

    2023-12-24 05:24:01       75 阅读