• 题目描述:

    You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

    You may assume the two numbers do not contain any leading zero, except the number 0 itself.

    Example:

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8
    Explanation: 342 + 465 = 807.
  • 思路分析:依旧温柔风的题儿~建个新链表,把两个链表求和撸一遍,需要注意求和时的进位问题
  • 代码实现:
    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    if(l1 == null || l2 == null){
    return l1 != null ? l1:l2;
    } int sum = l1.val + l2.val;
    ListNode result = new ListNode(sum % 10);
    result.next = addTwoNumbers(l1.next,l2.next); if(sum >= 10){//需要进位的情况
    result.next = addTwoNumbers(result.next,new ListNode(sum / 10));
    } return result;
    }
    }

    #现在的方法相当粗暴,现在给自己的目标是每天一题(并没有什么质量的那种啊啊啊),小陌考完四级要好好想想如何优化刷的题

Add Two Numbers--LeetCode进阶路②的更多相关文章

  1. Add Two Numbers LeetCode Java

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  2. Add two numbers [LeetCode]

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  3. Add Two Numbers ---- LeetCode 002

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. LeetCode 面试:Add Two Numbers

    1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  5. LeetCode 445. 两数相加 II(Add Two Numbers II)

    445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个 ...

  6. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  7. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. LeetCode Add Two Numbers II

    原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...

  9. leetcode 第二题Add Two Numbers java

    链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...

  10. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

随机推荐

  1. Typecho评论框加入七彩打字动画

    最终效果 使用步骤 对于 本主题,依次进入 控制台 - 外观 - 设置外观 - 主题自定义扩展,将以下代码加入到 自定义 HTML 元素拓展 - 在 body 标签结束前.其他主题,加入到主题对应的 ...

  2. 百万架构师第四十三课:Nginx:Nginx 应用实战|JavaGuide

    百万架构师系列文章阅读体验感更佳 原文链接:https://javaguide.net 公众号:不止极客 课程目标: Nginx 反向代理功能配置 Nginx 负载均衡实战 Nginx 动静分离配置 ...

  3. 大数据之路Week10_day04 (Hbase的二级索引,二级索引的本质就是建立各列值与行键之间的映射关系)

    二级索引的本质就是建立各列值与行键之间的映射关系 HBASE是在hadoop之上构建非关系型,面向列存储的开源分布式结构化数据存储系统. Hbase的局限性: HBase本身只提供基于行键和全表扫描的 ...

  4. Deepseek学习随笔(13)--- 清华大学发布第5弹:DeepSeek与AI幻觉(附网盘链接)

    人工智能技术的飞速发展为我们带来了前所未有的便利,但同时也伴随着一个不容忽视的问题--AI幻觉.清华大学发布的<DeepSeek与AI幻觉>详细探讨了AI幻觉的成因.评测方法以及应对策略, ...

  5. CF1693F题解

    备注 发表时间:2023-06-17 21:51 前言 yny 学长来 cdqz 讲课,写一篇讲课的题的题解纪念一下. 题意 给你一个 01 序列,有以下操作: 选择一段区间 设 \(cnt_0,cn ...

  6. RCE_STUDY

    概念 RCE(Remote code execution)远程代码执行漏洞,RCE又分命令执行和代码执行. RCE-远程代码执行:远程执行PHP代码 RCE-远程命令执行:远程执行Linux或者Win ...

  7. JMeter 获取 response body 的数据

    JMeter 获取 response body 的数据 位置:右键(HTTP Request) - Add - Post Processors - BeanShell PostProcessor im ...

  8. centos 防火墙配置,并限制端口

    查看防火墙状态 systemctl status firewalld 如果防火墙处于停止状态,则启动它: systemctl start firewalld 并设置防火墙开机自启: systemctl ...

  9. git clone加速

    使用github的镜像网站进行访问,github.com.cnpmjs.org,我们将原本的网站中的github.com 进行替换.

  10. 找到占用磁盘最多的文件或目录,可以使用du和sort

    想要找到占用磁盘最多的文件或目录,可以使用du和sort命令:   du -h /path/to/directory | sort -rh | head -n 10 其中: du -h /path/t ...