LeetCode Algorithm 02_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 linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Tags:Linked List, Math
分析:逐位相加,考虑进位。
- /**
- * 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) {
- if (!l1 || !l2) {
- return NULL;
- }
- int carry = ; //进位
- ListNode * result = new ListNode();
- ListNode * first = result; //追踪结果链表的当前节点
- ListNode * pre = NULL; //追踪结果链表的前一个节点
- //当l1和l2均没有超过链表末尾节点时
- while (l1 && l2) {
- first->val = (carry + l1->val + l2->val) % ;
- carry = (carry + l1->val + l2->val) / ;
- if (pre == NULL)
- pre = first;
- else {
- pre->next = first;
- pre = first;
- }
- first = new ListNode();
- l1 = l1->next;
- l2 = l2->next;
- }
- //当l1和l2都超过链表末尾节点时
- if (!l1 && !l2) {
- if (carry == ) {
- first->val = carry;
- pre->next = first;
- }
- return result;
- }
- //当l1超过末尾而l2尚未超过时
- if (!l1 && l2) {
- while (l2) {
- first->val = (carry + l2->val) % ;
- carry = (carry + l2->val) / ;
- if (pre == NULL)
- pre = first;
- else {
- pre->next = first;
- pre = first;
- }
- first = new ListNode();
- l2 = l2->next;
- }
- if (carry == ) {
- first->val = ;
- pre->next = first;
- }
- return result;
- }
- //当l2超过末尾而l1尚未超过时
- if (!l2 && l1) {
- while (l1) {
- first->val = (carry + l1->val) % ;
- carry = (carry + l1->val) / ;
- if (pre == NULL)
- pre = first;
- else {
- pre->next = first;
- pre = first;
- }
- first = new ListNode();
- l1 = l1->next;
- }
- if (carry == ) {
- first->val = ;
- pre->next = first;
- }
- return result;
- }
- }
- };
LeetCode Algorithm 02_Add Two Numbers的更多相关文章
- LeetCode Algorithm
LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- LeetCode Algorithm 05_Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- LeetCode 2 Add Two Numbers 模拟,读题 难度:0
https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two non-n ...
- LeetCode 2 Add Two Numbers(链表操作)
题目来源:https://leetcode.com/problems/add-two-numbers/ You are given two linked lists representing two ...
随机推荐
- 这是一套Java菜鸟到大牛的学习路线之高级教程,由工作了10年的资深Java架构师整理。
这是一套Java菜鸟到大牛的学习路线之高级教程,由工作了10年的资深Java架构师整理. 01-java高级架构师设计-基础深入 J2SE深入讲解 Java多 ...
- CF(438D) The Child and Sequence(线段树)
题意:对数列有三种操作: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. ...
- Netty In Action中文版 - 第七章:编解码器Codec
http://blog.csdn.net/abc_key/article/details/38041143 本章介绍 Codec,编解码器 Decoder,解码器 Encoder,编码器 Netty提 ...
- android 动态设置TextView值,例:金额添加
一说到动态递增设置TextView值,非常多人应该立即就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值可是效率不 ...
- jQuery Easy UI ProgressBar(进度条)组件
ProgressBar(进度条)组件,这个还是挺好玩的.我们在自己做点什么的时候常常能用到,比方上传下载文件.导入导出文档啊.加载网页等等. 应用场景非常多,使用起来还非常easy. 演示样例: &l ...
- Unity3D:粒子系统Particle System
1. GameObject → Create Other → Particle System. 2. 选中 Particle System,可看到下列屬性: 3.Particle System: ...
- Python 面向对象与 C++、Java 的异同
1. 子类是否自动调用父类的构造方法 C++.Java 会在子类对象的构造中自动首先调用父类的构造: Python 则相对啰嗦一点: 如果子类不覆盖父类的__init__()方法,则子类默认将执行与父 ...
- 关于node的fs路径问题
我在写一个静态网页的服务器中遇到的一个问题,当时没理解就去查了 因为要访问最外部的json文件,就定义了一个模块读取文件,然后在外边的server.js中调用 但是一直路径错误. 我相信很多人和我一样 ...
- Android 通过OnScrollListener来监听RecyclerView的位置
最近做一个漫画app,在阅读漫画界面需要通过获取recyclerView的位置来实时更新界面上的图片进度(比如1/9), 查阅资料得知了可以通过LayoutManager来获取recyclerView ...
- host---域名查询
host命令是常用的分析域名查询工具,可以用来测试域名系统工作是否正常. 选项 -a:显示详细的DNS信息: -c<类型>:指定查询类型,默认值为“IN“: -C:查询指定主机的完整的SO ...