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
题意解析:
给定两个链表表示两个非负数。数字逆序存储,每一个节点包括一个单一的数字。计算两个链表表示的数的和。并以相同格式的链表的形式返还结果。
解法就是直接操作链表相加就能够了。。
代码例如以下:
C++
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode * ans = NULL, *last = NULL;
int up = 0;
while (NULL != l1 && NULL != l2) {
int tmp = l1->val + l2->val + up;
up = tmp / 10;
if (NULL == last) {
ans = new ListNode(tmp % 10);
last = ans;
} else
last = pushBack(last, tmp % 10);
l1 = l1->next;
l2 = l2->next;
}
while (NULL != l1) {
int tmp = l1->val + up;
last = pushBack(last, tmp % 10);
up = tmp / 10;
l1 = l1->next;
}
while (NULL != l2) {
int tmp = l2->val + up;
last = pushBack(last, tmp % 10);
up = tmp / 10;
l2 = l2->next;
}
if (0 != up) {
ListNode * l = new ListNode(up);
last->next = l;
}
return ans;
}
ListNode * pushBack(ListNode * last, int val) {
ListNode * l = new ListNode(val);
last->next = l;
return l;
}
};
Python
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
carry = 0; head = ListNode(0); curr = head;
while l1 and l2:
Sum = l1.val + l2.val + carry
carry = Sum / 10
curr.next = ListNode(Sum % 10)
l1 = l1.next; l2 = l2.next; curr = curr.next
while l1:
Sum = l1.val + carry
carry = Sum / 10
curr.next = ListNode(Sum % 10)
l1 = l1.next; curr = curr.next
while l2:
Sum = l2.val + carry
carry = Sum / 10
curr.next = ListNode(Sum % 10)
l2 = l2.next; curr = curr.next
if carry > 0:
curr.next = ListNode(carry)
return head.next
水一枚。。。。
LeetCode第四题,Add Two Numbers的更多相关文章
- leetcode第四题--Add Two Numbers
Problem: You are given two linked lists representing two non-negative numbers. The digits are stored ...
- Leetcode 第 2 题(Add Two Numbers)
Leetcode 第 2 题(Add Two Numbers) 题目例如以下: Question You are given two linked lists representing two non ...
- 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解题笔记 - 2. Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode第二题—— Add Two Numbers(模拟两数相加)
Description: You are given two non-empty linked lists representing two non-negative integers. The di ...
- [算法题] Add Two Numbers
题目内容 题目来源:LeetCode You are given two non-empty linked lists representing two non-negative integers. ...
- LeetCode(2)Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- LeetCode之“链表”:Add Two Numbers
题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...
随机推荐
- DelphiXE7如何调用Java Class,JAR等文件?
源文地址:http://jingyan.baidu.com/article/e4d08ffdb61b040fd3f60d44.html 第一步,我们先在互联网上把java2pas这个工具下载下来. 下 ...
- [置顶] CSDN博客第四期移动开发最佳博主评选
CSDN博客第三期最佳移动开发博主评选圆满结束,恭喜所有上榜用户,为继续展示移动开发方向优秀博主,发掘潜力新星,为移动开发方向的博客用户提供平台,CSDN博客第四期移动开发最佳博主评选开始.同时,获奖 ...
- 中国市场 Android App 兼容性报告
由于手机操作系统的不同,以及操作系统版本之间的差异,使得真机测试这个过程尤其复杂,涉及终端.人员.工具.时间.管理等方面的问题,Android系统的设备因操作系统多样性和终端类型的庞杂,问题尤为复 ...
- 关于linux下内存使用的一些疑惑[转载]
Linux内存机制-Cache与Buffer 在linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉内存不够用了,其实不然.这是Linux内存管理的一个优秀特性,在这方面,区别于w ...
- java常量池理解
String类两种不同的创建方式 String s1 = "zheng"; //第一种创建方式 String s2 = new String("junxiang" ...
- 关于优化性能<主要是速度方面>的个人心得 【转】
一个web项目后期的维护主要在于性能方面.数据吞吐量一旦增大各种bug都出来了.那些通过硬件<数据库分表,数据库主从分离,读写分离>等的一些手段此处就不多说了.本文主要在编码方面做一个性能 ...
- [置顶] java web 动态服务器
写了一个java web 动态服务器,主要通过内部类来实现,动态类使用了外部类,采用了 classforname 实例化,动态类的构造方法不能带参数, 效果都出来了,分享给有需要的 朋友.判断做的不够 ...
- document.body is null
document.body is null:做前端的同学们对这个错误应该不陌生吧 出现这个问题的原因是:你太着急了,document还没渲染到body呢,你就想调用了,当然会找不到了 解决办法so e ...
- lucene+盘古分词
一般的网站都会有都会有搜索的功能,一般实现搜索主要有三种方案 第一种是最差的,也是最不推荐的,使用数据库的模糊查询例如select * form table where 字段 like XXX,这种查 ...
- OC 实现多选参数
在iOS的开发过程中有许多方法都是有可选参数的,例如: + (instancetype)arrayWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_N ...