LeetCode算法题——Add Two Numbers

SUMO从入门到精通:手把手教你用开源工具完成智能网联车仿真(附Python接口教程) 本文提供了一份SUMO开源交通仿真工具的完整实战指南,从环境搭建、基础仿真到Python接口TraCI的深度应用。重点讲解了如何利用SUMO的高度可定制性进行智能网联车仿真,包括实现动态信号优先等核心算法,并介绍了与网络仿真器集成以构建高保真车联网通信环境的方法。 阅读详情
Description:
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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

算法思想:创建链表,逐位相加,逢十进一,保存进位,加到下一位中运算
C++代码如下:
#include <iostream>
#include "stdlib.h"
using namespace std;
struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
    ListNode* addTwoNumbers(ListNode *l1, ListNode *l2) {
        int v1,v2,j=0,v;
        ListNode *head,*p1,*p2;
        head=p1=NULL;
        while(l1!=NULL||l2!=NULL){
            if(l1!=NULL){
                v1=l1->val;
                l1=l1->next;
            }else{
                v1=0;
            }
            if(l2!=NULL){
                v2=l2->val;
                l2=l2->next;
            }else{
                v2=0;
            }
            if(v1+v2+j<10){
                v=v1+v2+j;
                j=0;
            }else{
                v=v1+v2+j-10;
                j=1;
            }
            p2=(ListNode *)malloc(sizeof(ListNode));
            p2->val=v;
            p2->next=NULL;
            if(head==NULL){
                head=p1=p2;
            }else{
                p1->next=p2;
                p1=p1->next;
            }
        }
        if(j==1){
            p2=(ListNode *)malloc(sizeof(ListNode));
            p2->val=1;
            p2->next=NULL;   
            if(head==NULL){
                head=p1=p2;
            }else{
                p1->next=p2;
                p1=p1->next;
            }
        }
        return head;
    }
};

int main(){
    ListNode *l1h,*p1,*p2,*l2h,*q1,*q2,*res;
    p1=NULL;
    l1h=NULL;
    q1=NULL;
    l2h=NULL;
    int n,t,m,s;
    cout<<"please input the first List element number:";
    cin>>n;
    for(int i=0;i<n;i++){
        p2=(ListNode *)malloc(sizeof(ListNode));
        cin>>t;
        p2->val=t;
        p2->next=NULL;
        if(l1h==NULL){
            l1h=p1=p2;
        }else{
            p1->next=p2;
            p1=p1->next;
        }               
    }

    cout<<"please input the second List element number:";
    cin>>m;
    for(int i=0;i<m;i++){
        q2=(ListNode *)malloc(sizeof(ListNode));
        cin>>s;
        q2->val=s;
        q2->next=NULL;
        if(l2h==NULL){
            l2h=q1=q2;
        }else{
            q1->next=q2;
            q1=q1->next;
        }               
    }
//    while(l1h!=NULL){
//        cout<<l1h->val<<" ";
//        l1h=l1h->next;
//    }
//    cout<<endl;
//    while(l2h!=NULL){
//        cout<<l2h->val<<" ";
//        l2h=l2h->next;
//    }
    Solution sol;
    res=sol.addTwoNumbers(l1h,l2h);
    while(res!=NULL){
        cout<<res->val<<" ";
        res=res->next;
    }

}
STM32 热敏电阻测量温度 1、硬件 STM32F103C8T6、热敏电阻传感器、OLED。 2、热敏电阻传感器的介绍 阅读详情

相关推荐

Flocking for Multi-Agent Dynamic Systems: Algorithms and Theory的理论简述和python仿真实现(附源码)

经典论文Flocking for Multi-Agent Dynamic Systems: Algorithms and Theory的学习与编程仿真,提供源码

m0_59701926的博客 1238

leetcode: Add Two Numbers

题目: You are given two&nbsp;non-empty&nbsp;linked lists representing two non-negative integers. The digits are stored in&nbsp;reverse order&nbsp;and each of their nodes contain a single digit. Add the ...

时梦的博客 259

gerrit 配置 ----Gitweb集成

此外,为了让用户能够查看任何关于 gitweb 的信息 项目中,用户必须能够阅读所有引用(包括 refs/meta/config,refs/meta/dashboards/*等)。或者,如果Gerrit通过反向代理送达,它可以 为gitweb的链接生成不同的URL(需要的) 在网页服务器上重写为)。Gerrit 代码审查可以管理和生成指向 gitweb 的超链接, 允许用户从Gerrit内容跳转到相同信息, 但由 gitweb 显示。默认为,这是 Linux 发行版上“gitweb”包的常见安装路径。

QuanQuanYuanXin的博客 1405

LeetCode题解

Add Two Numbers 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 numbe

896

Scratch 3.0角色控制避坑指南:为什么你的键盘移动总卡顿?

本文深入剖析了Scratch 3.0中角色移动卡顿的根本原因,指出依赖`当按下`事件积木的局限性。通过对比分析,推荐采用`重复执行`结合`如果<键被按下?>`的循环控制方案,并引入速度变量模拟惯性,从而实现流畅、实时的键盘控制优化,显著提升游戏操作体验。

dd012的博客 1014

PowerBI参数化实战:3分钟搞定动态数据源切换(附常见错误排查)

本文详细介绍了PowerBI参数化实战技巧,教你3分钟实现动态数据源切换。通过创建参数、改造查询和测试切换三步,轻松应对多环境部署与团队协作中的路径问题,并附上常见错误排查指南,有效提升报表的可维护性和工作效率。

p1q2r的博客 585

LeetCode算法题之2:Add Two Numbers

LeetCode之2:Add Two Numbers 问题描述: 将两个个位数数字组成的链表合并为一个,模拟加法器。 问题的陷阱: 在考虑进位问题时想的不全名,漏掉两个链表最终同时为空,而产生进位,导致最终少加一位数这一问题。 缺陷代码分析 /** * Definition for singly-linked list. * public class ListNode { * in...

veatheroe的博客 235

华为OD机试真题 新系统 9月16号 【统计特殊数字】

华为OD新系统机试真题【统计特殊数字】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【统计特殊数字】。

qq_45776114的博客 769

【LeetCode】算法题2 Add Two Numbers的归纳

原题链接 题目: 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 numbe

Jojo 309

3 路由引入

在大型企业网络中,网络规模十分庞大,选用单一的路由协议无法满足网络的需求,因此多种路由协议共存的情况十分常见。或者出于业务逻辑或行政管理的考虑,会在不同的网络结构中设计和部署不同的路由协议,使路由的层次结构更加清晰可控。路由引入是具有方向性的,将路由信息从路由协议A引入到路由协议B(A-to-B),则路由协议B可获知A中的路由信息,但是此时,A还并不知晓B路由协议中的路由信息,除非配置B-to-A的路由引入。表3-1:各种路由协议的优先级。通过路由引入,可以实现路由信息在不同路由协议间传递。

lwljh134的博客 621

分布式系统中的“无状态”和“有状态”详解

有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如 tomcat 中的 session。例如登录:用户登录后,我们把登录者的信息保存在服务端 session 中,并且给用户一个 cookie 值,记录对应的 session。然后下次请求,用户携带 cookie 值来,我们就能识别到对应 session,从而找到用户的信息。

AI天才研究院 2万+

LeetCode算法题2:Add Two Numbers

You are given two linked lists representing two non-negative numbers. 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 linke

tianmingxin1991的博客 195

【实战项目开发技术分享】如何设置机器人禁行区/虚拟墙

在现代机器人技术的日益发展中,路径规划是使机器人在复杂环境中安全、高效移动的关键部分。其中一个重要的问题是如何划定机器人的禁行区,以确保机器人不会进入危险区域或无法到达的地方。在本文中,我们将探讨一些关键策略,帮助机器人算法工程师更好地划定禁行区。代价地图可以通过多层叠加来实现,一般包括以下几个层级:静态地图层(Static Map Layer):由SLAM(Simultaneous Localization and Mapping)构建的静态地图。该层包含了环境中固定不变的地物信息,如墙壁、家具等。

xiewf的博客 1万+

python写算法题:leetcode: 2. Add Two Numbers

https://leetcode.com/problems/add-two-numbers/#/description # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next =

485

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode 445. Add Two Numbers II--面试算法题--C++,Python解法

LeetCode 445. Add Two Numbers II–字节跳动面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部分题目C++和Python的解法都有。 题目地址:Add Two Numbers II - LeetCode You are given two non-empty linked...

个人博客 692

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

LeetCode OJ算法题(四):Add Two Numbers

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

op_yu的专栏 520

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

LeetCode 2. Add Two Numbers--C++,Python解法--面试算法题

LeetCode 2. Add Two Numbers–字节跳动面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部分题目C++和Python的解法都有。 题目地址:Add Two Numbers - LeetCode You are given two non-empty linked lists r...

个人博客 424

leetcode-2. Add Two Numbers简单的java链表算法题 面试碰到了吗?

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

beloveddarling的博客 587

LeetCode刷题总结python(持续更新中)

        我要用Python刷出自己的一片天空!         LeetCode习题集         题目 Two Sum Add Two Numbers Longest Substring Without Repeating Characters Median of Two Sorted Arrays Longest Palindromic Substring ZigZ...

shuaigezhou123的博客 5095

Leetcode题目解答汇总

Leetcode题目解答汇总   断断续续的在leetcode上刷了一些题,每次遇到一时解决不了的都跳过了,以至于没有完整的熟悉每个题。因此汇总在此,实时更新,方便继续刷题以及后期回顾。 1、001 Two Sum:http://blog.youkuaiyun.com/yefengzhichen/article/details/47020871 2、002 Add Two Numbers:htt

yefengzhichen的博客 1573

Leet Code 力扣 - - 最短最优雅python解法带解析汇总

Leet Code 刷题笔记 记录一下刷过的有意义和代表性的题 动态规划 Unique Paths 题目可以转换为排列组合问题,解是C(min(m,n), m+n),从m+n个中选出m个下移或n个右移。 用DP做很快,以后算 C(a, b) 都可以用算这题的DP法代替 ...

Knife丶小刀博客 4891

python中给定一个正整数,依次输出它的各位数

python中给定一个正整数,依次输出它的各位数步骤:应用于leetcode: *Add Two Numbers* 步骤: 1. 将当前正整数除以10,得到商和余数 2. 余数为当前正整数的个位数 3. 令商为当前正整数 4. 将当前正整数继续除以10,得到商和余数 5. 返回第二步,重复2,3,4直至得到的商为0 6. 将得到的余数依次输出,即为正整数各位数的反向输出 ...

王九思的博客 1万+

ARTS-for-week5-20181116

每周完成一个ARTS:每周至少做一个 leetcode 的算法题、阅读并点评至少一篇英文技术文章、学习至少一个技术技巧、分享一篇有观点和思考的技术文章。(也就是 Algorithm、Re...

herongwei 的 BLOG 366

leetcode2--add two numbers(两数之和)

思路为对应位相加,用flag数组存进位信息,ans数组存每个位处理后的数值,处理每个位包括两种情况:一个是低位进1该位加1,一个是该位要进位减10。处理完每个位后还要注意最高位的进位:如果最高位有进位的话是要产生一个新的最高位。最开始的想法是将两个链表给的信息转化为整数,再将两个整数相加,最后将和转化为一个链表。最后想说的一点是对两个链表的处理,如果两个链表等长,那没啥好说的。先处理两个链表均不为空的部分,再处理剩下的部分。在力扣中,这道题结点数在【1,100】内,因此ans数组可设的比100大点。

2301_81436696的博客 627

LeetCode //C - 445. Add Two Numbers II

【代码】LeetCode //C - 445. Add Two Numbers II。

Made in Code 1897

Leetcode[2] Add Two Numbers and Leetcode[445] Add Two Numbers II

Leetcode[2] Add Two Numbers class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ ...

只为分享 252
上一篇: LeetCode算法题——ZigZag Conversion
下一篇: LeetCode算法题——Reverse Integer
LZTree
博客等级 码龄12年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值