addTwoNumbers
大神的代码好短,自己写的120多行=_= 各种判断
ListNode *f(ListNode *l1, ListNode *l2) {
ListNode *p1 = l1;
ListNode *p2 = l2;
ListNode *res = new ListNode();
ListNode *p3 = res;
int sum = ;
while (p1 != NULL || p2 != NULL) { //这里用“或”
sum /= ; //进位
if (p1 != NULL) { //这样一来位数不同的就不用分开讨论了
sum += p1->val;
p1 = p1->next;
}
if (p2 != NULL) {
sum += p2->val;
p2 = p2->next;
}
p3->next = new ListNode(sum % ); //不用新建指针
p3 = p3->next;
}
if (sum >= ) {
p3->next = new ListNode();
}
return res->next; //这样就省了不少讨论
}
addTwoNumbers的更多相关文章
- add-two-numbers
leetcode开篇~ 问题描述: You are given two linked lists representing two non-negative numbers. The digits a ...
- 【leedcode】add-two-numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- Leetcode解题-链表(2.2.1)AddTwoNumbers
1 题目:2.2.1 Add Two Numbers You are given two linked lists representing two non-negative numbers. The ...
- leetcode — add-two-numbers
import java.util.ArrayList; import java.util.LinkedList; import java.util.List; /** * Source : https ...
- LeetCode-2AddTwoNumbers(C#)
# 题目 2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The di ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
随机推荐
- XSS
XSS的含义 XSS(Cross Site Scripting)即跨站脚本.跨站的主要内容是在脚本上. 跨站脚本 跨站脚本的跨,体现了浏览器的特性,可以跨域.所以也就给远程代码或者第三方域上的代码提供 ...
- Angular源码分析之$compile
@(Angular) $compile,在Angular中即"编译"服务,它涉及到Angular应用的"编译"和"链接"两个阶段,根据从DO ...
- 升级npm
查看npm的所有版本 运行命令: npm view npm versions 命令运行后,会输出到目前为止npm的所有版本. [ '1.1.25', '1.1.70', '1.1.71', '1.2. ...
- 在开源中国(oschina)git中新建标签(tags)
我今天提交代码到主干上面,本来想打个标签(tags)的. 因为我以前新建过标签(tags),但是我现在新建的时候不知道入库在哪了.怎么找也找不到了. 从网上找资料也没有,找客服没有人理我,看到一个交流 ...
- x01.os.22: ubuntu 常用设置
新组装了个 64 位电脑,i5 CPU,进入 ubuntu 后,又是一通搜索设置,整理如下,以备后用. 安装 .dep 包 sudo dpkg -i [filename.dep] 在 ubuntu 中 ...
- 解决Ubuntu Kylin 1610安装ANSYS17.2的NVIDIA显卡驱动问题
Ubuntu Kylin 1610在安装完毕后,会自动安装显卡驱动,对于一般的图形图像使用来说自然不会有太大的问题,但是对于ANSYS17.2的一些模块,还是会出现问题.一个比较常见的问题就是Open ...
- 使用四元数解决万向节锁(Gimbal Lock)问题
问题 使用四元数可以解决万向节锁的问题,但是我在实际使用中出现问题:我设计了一个程序,显示一个三维物体,用户可以输入绕zyx三个轴进行旋转的指令,物体进行相应的转动. 由于用户输入的是绕三个轴旋转的角 ...
- SpringMVC初始化参数绑定--日期格式
一.初始化参数绑定[一种日期格式] 配置步骤: ①:在applicationcontext.xml中只需要配置一个包扫描器即可 <!-- 包扫描器 --> <context:comp ...
- centos7+mono4.2.3.4+jexus5.8.1跨平台起飞
很早之前就开始关注.net跨平台,最近正好测试了下用EF6连接mysql,于是就想直接把网站扔进Linux.查了很多资料,鼓捣了两个晚上,终于成功. 这里我使用的是budgetvm的1G openvz ...
- 在.NET Core控制台程序中使用依赖注入
之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...