【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代码: ...
随机推荐
- spark伪分布式安装
一,在官网下载对应的版本http://spark.apache.org/downloads.html 二在linux中解压下来的spark包 三:配置环境变量 (1)在/etc/profi ...
- Pyqt SpVoice朗读功能
用Pyqt 做一个读取系统剪贴板内容,然后通过语音合成(TTS)朗读出剪贴板的内容 知识要点 SpVoice SpVoice类是支持语音合成(TTS)的核心类.通过SpVoice对象调用TTS引擎,从 ...
- iframe无刷新跨域并获得返回值
参考:http://geeksun.iteye.com/blog/1070607 /** * iframe跨域提交大数据 * @param action 跨域地址 * @param arr [ {na ...
- 国内其他的maven库
转自:http://www.cnblogs.com/woshimrf/p/5860478.html 在oschina关来关去的烦恼下,终于受不了去寻找其他公共库了. 阿里云maven镜像 <mi ...
- Visual Studio 插件的开发(转)
起因 在做项目的时候,经常需要根据表结构create一些实体类,写多了,实在是觉得无趣,于是就琢磨着做个代码生成工具.当然现在有很多现成的,拿来用就好,可是总想自己弄个出来玩玩,一来是当初用DataS ...
- java 猜数字游戏
作用:猜数字游戏.随机产生1个数字(1~10),大了.小了或者成功后给出提示. 语言:java 工具:eclipse 作者:潇洒鸿图 时间:2016.11.10 >>>>> ...
- nodejs 提示‘xxx’ 不是内部或外部命令解决方法
本文介绍了node.js包管理工具npm安装模块后,无法通过命令行执行命令,提示‘xxx’ 不是内部或外部命令的解决方法,给需要的小伙伴参考下. 一般出现这样的问题原因是npm安装出现了问题,全局 ...
- [超级懒人最简单法]iPhone 6 plus 适配切图方法分享(转载文章)
网络上已经有很多适配教程,可是看了半天总是半懂不懂..最后还是要综合多个教程再动动脑子动动手,最好有程序大哥帮你试一下(这得有多大的福气) 如果有跟我一样情况的: 1. 有人说用sketc ...
- 转:ibatis的cacheModel
转:ibatis的cacheModel cachemodel是ibatis里面自带的缓存机制,正确的应用能很好提升我们系统的性能. 使用方法:在sqlmap的配置文件中加入 <cacheMode ...
- c#操作mysql积累
1,连接字符串 Server=localhost;port=;User ID=root;password=admin;database=;charset=utf8;Allow User Variabl ...