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 ...
随机推荐
- C++11 volatile 类型
volatile作用: 作为指令关键字,确保本条指令不会受到编译器的优化而省略,而且要求每次直接读值. 定义: volatile int nTest; volatile关键字是一种类型修饰符,用它声明 ...
- promise的弊端
promise的弊端 promise彻底解决了callback hell,但也存在以下一些问题 延时问题(涉及到evnet loop)(http://www.ruanyifeng.com/blog/2 ...
- 怎样更好的设计android图标,拉伸不变形等等系列长处,并且减小apk大小
android mvp框架:dileber(https://github.com/dileber/dileber.git) 继续为大家介绍android mvp开源框架 dileber 今天主要是字 ...
- js---04 属性 this
var oUl = document.getElementsByTagName('ul')[0]; var aLi = oUl.getElementsByTagName('li'); window.o ...
- GridDataView实现 点击任意一格可以修改
直接上代码好了 private void dgv1Member_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { string ...
- CMDB学习之四 ——DEBUG模式
定义一个debug,进行解析调试,到测试文件 配置文件,配置debug模式,定义环境变量, #!/usr/bin/env python # -*- coding:utf-8 -*- import os ...
- 今日SGU 5.10
SGU 168 题意:从a矩阵求出b矩阵,规则直接看题目就行了,不好打字说明 收获:dp #include<bits/stdc++.h> #define de(x) cout<< ...
- 阅读笔记—MVC
MVC设计模式 Model 1 体系结构 在Model 1 体系结构中,每一个请求的目标都是JSP页面.JSP页面负责完成请求所需要的而所有任务,其中包括验证客户.使用JavaBeans访问数据库及管 ...
- ArcSDE:C#创建SDE要素数据集
转自原文 ArcSDE:C#创建SDE要素数据集 /// <summary> /// 判断指定数据集是否存在 /// </summary> /// <param name ...
- HDU 5063 Operation the Sequence(暴力 数学)
题目链接:pid=5063" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=5063 Prob ...