LeetCode之“链表”: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
这道题难度不大,具体程序如下:
/**
* 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)
return l2;
else if(!l2)
return l1; ListNode *dummy = new ListNode();
ListNode *head = nullptr, *start = dummy;
int carry = ;
while(l1 || l2)
{
if(l1 && l2)
{
int sum = l1->val + l2->val + carry;
l1->val = sum % ;
carry = sum / ;
start->next = l1;
l1 = l1->next;
l2 = l2->next;
}
else if(l1)
{
int sum = l1->val + carry;
l1->val = sum % ;
carry = sum / ;
start->next = l1;
l1 = l1->next;
}
else
{
int sum = l2->val + carry;
l2->val = sum % ;
carry = sum / ;
start->next = l2;
l2 = l2->next;
} start = start->next;
} if(carry)
{
ListNode *node = new ListNode();
start->next = node;
} head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};
LeetCode之“链表”:Add Two Numbers的更多相关文章
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- C# 写 LeetCode Medium #2 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode OJ:Add Two Numbers (相加链表之数)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode】2.Add Two Numbers 链表数相加
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 【一天一道leetcode】 #2 Add Two Numbers
一天一道leetcode系列 (一)题目: You are given two linked lists representing two non-negative numbers. The digi ...
- leetcode题解2. Add Two Numbers
题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ...
- leetcode@ [2/43] Add Two Numbers / Multiply Strings(大整数运算)
https://leetcode.com/problems/multiply-strings/ Given two numbers represented as strings, return mul ...
随机推荐
- mysql字符集,insert,update,delete,select
发现有错误:数据太长了.//查看数据库的所有编码:show variables like 'character%';-----+| character_set_client | utf8 ...
- mysql进阶(二十九)常用函数
mysql进阶(二十九)常用函数 一.数学函数 ABS(x) 返回x的绝对值 BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制) CEILING(x) 返回大于x的最小整数值 EXP ...
- EBS业务学习之应付INVOICE类型
INVOICE类型 类 型 描 述 标准INVOICE 是指由于采购货物或接受劳务,从供应商处取得的INVOICE (标准INVOICE,既可以和订单匹配,也可以不匹配) ...
- Dynamics CRM2016 站点地图Bug之KnowledgeArticle不显示
在CRM2016中对知识库功能进行了加强,新加入了KnowledgeArticle的实体,但在学习的过程中发现了微软的一个bug 下图是我在最新的2016online的版本上看到的service下的s ...
- FFmpeg的H.264解码器源代码简单分析:概述
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
- (一二四)tableView的多组数据展示和手动排序
最近在写一个轻量级的网络游戏,遇到了技能优先顺序手动排序的需求,我就想到了iOS自带的tableView编辑功能,对其进行了初步探索,最后做出的效果如下图所示: 点击左边可以删除,拖住右边可以手动排序 ...
- 如何在web.xml文件中引入其他的xml文件(拆分web.xml)
转载自:http://www.blogjava.net/jiangjf/archive/2009/04/09/264685.html 最近在做一个Servlet+javaBean的项目,服务器用的是t ...
- iOS中 为 iOS 建立 Travis CI 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 你是否曾经试着为 iOS 项目搭建一台支持持续集成的服务器,从我的个人经验而言,这可不是一个轻松的活.首先需要准备 ...
- RMI方式Ehcache集群的源码分析
Ehcache不仅支持基本的内存缓存,还支持多种方式将本地内存中的缓存同步到其他使用Ehcache的服务器中,形成集群.如下图所示: Ehcache支持多种集群方式,下面以RMI通信方式为例,来具体分 ...
- Android初级教程人品计算器
先看布局: main_activity.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr ...