【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代码: ...
随机推荐
- 2.Java异常学习
1.Java异常的概念 异常的例子 1.除法就是一个需要捕获异常的例子,除数又可能是0 异常处理的基本流程如下 一旦发生异常,就使得程序不按照原来的流程继续的运行下去 a.程序抛出异常 try{ th ...
- Android真机调试 Android.Util.AndroidRuntimeException: You cannot combine custom titles with other title features
参考连接:http://blog.csdn.net/scyatcs/article/details/9003285 Android.Util.AndroidRuntimeException: You ...
- hdu1561 The more, The Better (树形dp+背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...
- 浏览器User-agent简史(user-agent)
In the beginning there was NCSA Mosaic, and Mosaic called itself NCSA_Mosaic/2.0 (Windows 3.1), and ...
- inner join on 和 where = 的区别!
请看下面两条语句:select * from table1 inner join table2 on table1.id = table2.idselect * from table1,table2 ...
- Linux琐碎
本周接触Linux的内容: 1.netstat -tanlp 显示监听的所有端口并且不解析端口为属于哪个进程 history | grep cmd 从命令历史中找到需要的命令 2. scp命令的使用: ...
- Jenkins + Ant + Git + Tomcat自动化部署
环境linux下,大致的配置内容如下: 首先安装JDK配置环境变量等. 其次安装ANT配置ANT_HONE并把bin目录加入PATH中. 然后安装Git,并生成sshkey配置ssh 安装tomcat ...
- Eclipse,到了说再见的时候了——Android Studio最全解析
转自:http://blog.jobbole.com/77635/ 去年的Google大会上,Google带给我们一个小玩具——Android Studio,说它是玩具,是因为它确实比较菜,界面过时, ...
- Hint when use HTTPAgilityPack
1- Read the usage policy of the website. I know this is the third time I mention that, but that tell ...
- java接口的嵌套
java接口 1.接口中定义的变量默认是public static final 型,且必须给其初值,所以实现类中不能重新定义,也不能改变其值 2.接口中的方法默认都是 public abstract ...