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. tensorflow的安装

    binary安装(推荐) 注意需要能访问外网 Install pip (or pip3 for python3) if it is not already installed: # Ubuntu/Li ...

  2. Mybatis实现数据的增删改查(CRUD)

    什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索.MyBat ...

  3. 8-04流程控制语句BEGIN ..END

     流程控制语句: 是用来控制程序流程的语句. 常用的流程控制语句的分类: 顺序结构:BEGIN...END 分支结构: IF ..ELSE 或CASE ..END 循环结构:WHILE 顺序结构 语法 ...

  4. TreeView checkbox 全选

    在使用TreeView 控件 ,进行权限管理的时候,需要使用 checkbox全选. 勾选父节点,子节点全部选中.取消父节点,子节点不选中. 勾选子节点,父节点也选中. 以下是在使用的例子: < ...

  5. T-SQL 基础学习 02

    数据库设计 定义 数据库设计就是将数据库中的数据实体以及这些数据实体之间关系,进行规划和结构化的过程 在需求分析阶段,设计数据库的一般步骤 A. 收集相信 B. 标识实体 C. 标记每个实体需要存储的 ...

  6. 【oracle】oracle表结构导出到Word

    因为需要写数据库文档,所以需要把数据库里边的表结构在word中用表格列出来,之前一直用powerdesigner,感觉有些麻烦,后来在网上找到了一段sql语句,经测试完全符合我的需求,不敢独享,语句如 ...

  7. 虚拟机和windows主机中的文件共享

    22:54 2015/12/22 虚拟机和windows主机中的文件共享:特别推荐:我的一个老师特别推荐的方法:在windows安装SSH Secure File Transfer Client,直接 ...

  8. ZeroMQ接口函数之 :zmq_z85_decode – 从一个用Z85算法生成的文本中解析出二进制密码

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_z85_decode zmq_z85_decode(3)         ØMQ Manual - ØMQ/4.1 ...

  9. SQL行合并

    CREATE TABLE SC ( Student ), Course ) ) INSERT INTO SC SELECT N'张三',N'大学语文' UNION ALL SELECT N'李四',N ...

  10. java分享第六天(冒泡排序)

    冒泡排序 基本思想: 在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即:每当两相邻的数比较后发现它们 的排序与排序要求 ...