Leetcode—剑指Offer LCR 025.两数相加II【中等】

2023每日刷题(六十七)

Leetcode—LCR 025.两数相加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. leetcode100-028】【链表】相加

    2023-12-24 12:02:01       59 阅读

最近更新

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

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

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

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

    2023-12-24 12:02:01       96 阅读

热门阅读

  1. mysql参数配置binlog

    2023-12-24 12:02:01       48 阅读
  2. 【FLink消费Kafka之FlinkConsumer到KafkaSource的转变】

    2023-12-24 12:02:01       68 阅读
  3. Golang make vs new

    2023-12-24 12:02:01       60 阅读
  4. docker 安装mysql 8.0.35

    2023-12-24 12:02:01       47 阅读
  5. 力扣labuladong——一刷day78

    2023-12-24 12:02:01       65 阅读
  6. python自动开发,基础2

    2023-12-24 12:02:01       48 阅读
  7. Vue 3 表单处理精讲:打造响应式注册表单的艺术

    2023-12-24 12:02:01       54 阅读
  8. ClickHouse(19)ClickHouse集成Hive表引擎详细解析

    2023-12-24 12:02:01       47 阅读
  9. pytorch常用的几个函数详解

    2023-12-24 12:02:01       52 阅读
  10. [网络安全] 远程登录

    2023-12-24 12:02:01       57 阅读
  11. Vite和Webpack的优缺点

    2023-12-24 12:02:01       65 阅读
  12. Nodejs+Express搭建HTTPS服务

    2023-12-24 12:02:01       55 阅读