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的更多相关文章

  1. 【LeetCode445】 Add Two Numbers II★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以正序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(7->2->4->3)(7243) + ...

  2. 【leetcode】Add Two Numbers

    题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  3. 【题解】【链表】【Leetcode】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【leetcode】Add Two Numbers(middle) ☆

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  5. 【leetcode】 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. 【链表】Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  7. 【Leetcode】【Medium】Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  9. 【LeetCode2】Add Two Numbers★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(2->4->3)(342) + (5-> ...

  10. 【LeetCode67】 Add Binary

    题目描述: 解题思路: 此题的思路简单,下面的代码用StringBuilder更加简单,注意最后的结果要反转过来.[LeetCode415]Add Strings的解法和本题一模一样. java代码: ...

随机推荐

  1. 解决IE8 内置JSON.stringify,中文变unicode的问题

    转自:http://my.oschina.net/u/919074/blog/191131 项目中出现在IE下出现把json对象转为json串中文变成unicode的问题,最后经过排查,发现是IE8内 ...

  2. springMVC 学习(一)

    本文主要介绍springmvc的框架原理,并通过一个入门程序展示环境搭建,配置以及部署调试. springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合 ...

  3. jq表头固定

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta na ...

  4. Loadrunner监控Apache

    一.安装Apache yum -y install httpd 二.配置Apache 1.设置开机启动 chkconfig httpd on 2.开启Apache service httpd star ...

  5. 跟随Rodolfo进入VR时代!

    大家好,我是Rodolfo!一个热衷于Virtual Reality (VR),Operating System,Flushbonading和Algorithm的IT行业CTO. 基于多年的自我喜好研 ...

  6. nuget packages batch install

    d:\nuget\nuget.exe install EnterpriseLibrary.Common -NoCache -Verbosity detailed -OutputDirectory D: ...

  7. 限制Xamarin获取图片的大小

    限制Xamarin获取图片的大小在App开发中,经常会使用网络图片.因为这样不仅可以减少App的大小,还可以动态更新图片.但是手机使用网络环境千差万别.当网络环境不是理想的情况下,加载网络图片就是一个 ...

  8. Codeforces 307 div2 E.GukiZ and GukiZiana 分块

    time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...

  9. VS2010 有关测试的一些使用

    Visual Studio 2010 单元测试之一---普通单元测试:http://blog.csdn.net/tjvictor/archive/2011/02/09/6175362.aspx   V ...

  10. dev checkbox多选

    GridControl如果要支持多选,设置Options->OptionSeletion->MultiSelet为true就ok.