题目链接:https://leetcode.com/problems/add-two-numbers/description/

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

思路:

  • 用两个结点指针p1、p2分别指向这两个链表L1、L2
  • p1、p2两者指向非空时将p1、p2所指向结点的值val和进位数carNum相加。即sum = p1->val + p2->val + carNum; 其中进位数carNum初始值为0。
    1. 更新进位数的值:carNum = sum / 10;
    2. 获得当前位的值:cur = sum % 10;
  • p1 p2 两者指向至少有一个为空时,上一步的操作终止。对非空的链表继续进行后续操作。

     

编码如下

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *p1 = l1;
ListNode *p2 = l2; ListNode *pHead = new ListNode(-);
ListNode *p = nullptr; int carNum = ; // 进位数 while (nullptr != p1 && nullptr != p2)
{
int sum = p1->val + p2->val + carNum;
carNum = sum / ; // 求进位数
int cur = sum % ; // 当前位的数字 ListNode *pTemp = new ListNode(cur); if (p == nullptr)
{
p = pTemp;
pHead->next = p;
}
else
{
p->next = pTemp;
p = pTemp;
} p1 = p1->next;
p2 = p2->next;
} while (nullptr != p1)
{
int sum = p1->val + carNum;
carNum = sum / ;
int cur = sum % ; ListNode *pTemp = new ListNode(cur); if (p != nullptr)
{
p->next = pTemp;
p = pTemp;
} p1 = p1->next;
} while (nullptr != p2)
{
int sum = p2->val + carNum;
carNum = sum / ;
int cur = sum % ; ListNode *pTemp = new ListNode(cur); if (p != nullptr)
{
p->next = pTemp;
p = pTemp;
} p2 = p2->next;
} if (carNum != )
{
ListNode *pTemp = new ListNode(carNum);
if (p != nullptr)
{
p->next = pTemp;
p = pTemp;
}
} return pHead->next; }
};

002. Add Two Numbers的更多相关文章

  1. No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

  2. LeetCode #002# Add Two Numbers(js描述)

    索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ...

  3. LeetCode--No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

  4. leetcode刷题: 002 Add Two Numbers

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

  5. 【JAVA、C++】LeetCode 002 Add Two Numbers

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

  6. 【LeetCode】002 Add Two Numbers

    题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  7. 002 Add Two Numbers 链表上的两数相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  8. [Leetcode] 002. Add Two Numbers

    https://leetcode.com/problems/add-two-numbers/ public class Solution { public ListNode addTwoNumbers ...

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

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

随机推荐

  1. Summer training #7

    B:读懂题意模拟 #include <bits/stdc++.h> #include <cstring> #include <iostream> #include ...

  2. git撤销pull命令

    1.运行git reflog命令查看你的历史变更记录 2.然后用git reset --hard HEAD@{n},(n是你要回退到的引用位置)回退. 比如上图可运行 git reset --hard ...

  3. MyBatis-01-简介

    基础知识: JDBC Mysql Java基础 Maven Junit 框架:是有配置文件的.最好的方式:看官网文档 1.简介 1.1.什么是MyBatis 简介 什么是 MyBatis? MyBat ...

  4. SSM框架中使用日志框架

    在 pom,xml 配置 Log4j jar 添加一个 mybatis_log.xml 文件 完整配置信息 <?xml version="1.0" encoding=&quo ...

  5. fastclick无需对IOS11及以上做处理

    // 解决click点击300毫秒延时问题 (IOS11及以上无需处理) import FastClick from 'fastclick' const device = navigator.user ...

  6. Python-multiprocessing-Pool模块

    from multiprocessing import Pool import os import time import random def long_time_task(name): print ...

  7. js 实现深拷贝

    在ECMAScript变量中包含两种不同类型的值:基本类型值和引用类型值. 基本类型值:Undefined.Null.Boolean.Number.String 引用类型值:Object.Array. ...

  8. C# 检测 代码耗时

    static void SubTest() { Stopwatch sw = new Stopwatch(); sw.Start(); //耗时巨大的代码 sw.Stop(); TimeSpan ts ...

  9. windows查看服务的状态

    方法一:运行窗口操作 按下win+r键,在运行窗口中输入services.msc,点击[确定]按钮,即可打开服务. 如下图所示: 方法二:按部就班 1)此电脑—右键—管理 2)点击[服务和应用程序]按 ...

  10. [Python之路] 使用装饰器给Web框架添加路由功能(静态、动态、伪静态URL)

    一.观察以下代码 以下来自 Python实现简易HTTP服务器与MINI WEB框架(利用WSGI实现服务器与框架解耦) 中的mini_frame最后版本的代码: import time def in ...