码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 每日一题《leetcode-- LCR 025.两数相加||》


    https://leetcode.cn/problems/lMSNwu/


    分别把给定的两个链表翻转,然后从头开始相加。

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * struct ListNode *next;
    6. * };
    7. */
    8. //反转链表
    9. struct ListNode* reverselist(struct ListNode*h1)
    10. {
    11. struct ListNode* newhead = NULL;
    12. struct ListNode* cur = h1;
    13. while(cur)
    14. {
    15. struct ListNode* next = cur->next;
    16. cur->next = newhead;
    17. newhead = cur;
    18. cur = next;
    19. }
    20. return newhead;
    21. }
    22. struct ListNode* add(struct ListNode*head1 , struct ListNode* head2)
    23. {
    24. //返回的新链表的头和尾
    25. struct ListNode* head = NULL,* tail = NULL;
    26. //进位
    27. int carry = 0;
    28. while(head1 || head2)
    29. {
    30. int n1 = head1? head1->val : 0;
    31. int n2 = head2? head2->val : 0;
    32. int sum = n1 + n2 + carry;
    33. if(head == NULL)
    34. {
    35. head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
    36. tail->val = sum % 10;
    37. tail->next = NULL;
    38. }
    39. else
    40. {
    41. tail->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    42. tail->next->val = sum % 10;
    43. tail = tail->next;
    44. tail->next = NULL;
    45. }
    46. carry = sum / 10;
    47. if(head1)
    48. {
    49. head1 = head1->next;
    50. }
    51. if(head2)
    52. {
    53. head2 = head2->next;
    54. }
    55. }
    56. if(carry > 0)
    57. {
    58. tail->next = (struct ListNode*)malloc(sizeof(struct ListNode));
    59. tail->next->val = carry;
    60. tail = tail->next;
    61. tail->next = NULL;
    62. }
    63. return head;
    64. }
    65. struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
    66. l1 = reverselist(l1);
    67. l2 = reverselist(l2);
    68. struct ListNode* l3 = add(l1,l2);
    69. return reverselist(l3);
    70. }

  • 相关阅读:
    【2022牛客多校-2】G Link with Monotonic Subsequence
    堆排序算法
    4-3-网络架构和Netty系列-Netty通讯框架总体架构设计
    金仓数据库 KingbaseGIS 使用手册(9.1. 拓扑类型、9.2. 拓扑域函数、9.3. 拓扑和拓扑几何对象管理)
    RabbitMQ运行机制和通讯过程介绍
    C#_Win32_PInvoke源码生成器
    django学习入门系列之第二点《浏览器能识别的标签3》
    通过后端数据交互,实现【会议Oa小程序】首页数据渲染
    Kotlin高仿微信-项目实践58篇
    chrome Driver 使用教程
  • 原文地址:https://blog.csdn.net/weixin_73359101/article/details/139390713
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号