【leedcode】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
两数之和(考察链表操作):
链表长度不一,有进位情况,最后的进位需要新增节点。
int up = 0;
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
ListNode newList = new ListNode(-1);
ListNode newNode, rootList = newList;
while (l1 != null && l2 != null) {
newNode = new ListNode(l1.val + l2.val + up);
up = newNode.val /10;
newNode.val = mod(newNode.val,10, up);
newList.next = newNode;
newList = newList.next;
l1 = l1.next;
l2 = l2.next;
}
newList = loopList(l1, newList);
newList = loopList(l2, newList);
if (up > 0) {
newList.next = new ListNode(up);
}
return rootList.next;
} ListNode loopList(ListNode loopList, ListNode newList) {
ListNode newNode;
while (loopList != null) {
newNode = new ListNode(loopList.val + up);
up = newNode.val /10;
newNode.val = mod(newNode.val, 10, up);
newList.next = newNode;
newList = newList.next;
loopList = loopList.next;
}
return newList;
}
static int mod(int x, int y, int v) {
return x - y * v;
}
码出来的代码不不如答案,哎,超级简洁:
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(mod(sum , 10 , carry));
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
【leedcode】add-two-numbers的更多相关文章
- 【LeetCode445】 Add Two Numbers II★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...
- 【leetcode】Add Two Numbers
题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【题解】【链表】【Leetcode】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】Add Two Numbers(middle) ☆
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【leetcode】 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【链表】Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- 【Leetcode】【Medium】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【LeetCode】Add Two Numbers(两数相加)
这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...
- 【LeetCode2】Add Two Numbers★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(2->4->3)(342) + (5-> ...
- 【LeetCode67】 Add Binary
题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...
随机推荐
- Windows下安装Redis
1.首先,Redis官方是支持Linux系统的,我这里不多说,需要的可以参考:http://www.oschina.net/question/12_18065/ 2.Windows 64位下载地址:h ...
- 自定义RatingBar,不同分辨率屏幕下图片拉伸或者显示不完整问题解决
1.需要两张评分图片ic_rating_highlight.png ic_rating_normal_white.png(宽高都是52px,且有内边距) 将这两张图片添加到各分辨率文件夹下 开发过 ...
- Linux虚拟机添加新硬盘的全程图解
查看网的文章,我将在vm虚拟机LinuxRedhat中添加一个新的硬盘, 过程大致如下: 1.选择"VM"----"setting"并打开,将光标定位在hard ...
- 8-05分支结构CASE..END
语法: CASE WHEN 条件1 THEN 结果1 WHEN 条件2 THEN 结果2 ...ELSE 其他结果 END 执行顺序: 条件1成立执行结果1 条件2成立执行结果2 如果所有WH ...
- Oracle协议适配器错误解决办法
在Oracle中新建了一个数据库,今天把它删了之后再登录SQL*PLUS就登不上去了,出现ORA-12560:TNS:协议适配器错误. ORA-12560: TNS: 协议适配器错误的解决方法 造成O ...
- 解决 卸载Mysql后,服务还在的问题
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Application\MySQL文件夹:删除HKEY_LOCAL_MACHINE\ ...
- jquery扩展(- _ -//
如何写一个Jquery 之前看了好多网上的例子,讲真,不知其所以然,生搬硬套.其实自己还是一脸的懵B,想想还真的有必要仔细研读一下书籍. 言归正传,正式巴拉巴拉笔记 扩展Jquery什么鬼??? 三个 ...
- CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort 暴力枚举,水 1.题意:n*m的数组, ...
- ZeroMQ接口函数之 :zmq_tcp – 使用TCP协议的ØMQ网络单播协议
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq-tcp zmq_tcp(7) ØMQ Manual - ØMQ/4.1.0 Name zmq_t ...
- RunLoop和autorelease的一道面试题
有这么一道iOS面试题 以下代码有没有什么问题?如果有?如何解决? for (int i = 0; i < largeNumber; i++) { NSString *str = [NSStri ...