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

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode c1 = l1;//定义两个指针变量 c1 c2
ListNode c2 = l2;
ListNode head = new ListNode(0);//定义一个新链表的头结点
ListNode d = head;//新链表的指针变量
int sum = 0;
while(c1 != null || c2 != null) {
if(c1 != null) {
sum = sum + c1.val;
c1 = c1.next;
}
if(c2 != null) {
sum = sum + c2.val;
c2 = c2.next;
}
d.next = new ListNode(sum%10);
d = d.next;
sum = sum /10;
}
if(sum == 1) {
d.next = new ListNode(1);
}
return head.next; }
}

2.Add Two Numbers-两个单链表相加的更多相关文章

  1. 面试题:Add Two Numbers(模拟单链表)

    题干: You are given two non-empty linked lists representing two non-negative integers. The digits are ...

  2. [LeetCode] Add Two Numbers 两个数字相加

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

  3. [CareerCup] 2.5 Add Two Numbers 两个数字相加

    2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...

  4. [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现

    [LeetCode] Add Two Numbers 两个数字相加   You are given two non-empty linked lists representing two non-ne ...

  5. python经典面试算法题1.3:如何计算两个单链表所代表的数之和

    本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.2 如何实现链表的逆序 [华为笔试题] 难度系数:⭐⭐⭐ ...

  6. [LeetCode] 2. Add Two Numbers 两个数字相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  7. LeetCode(2):Add Two Numbers 两数相加

    Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...

  8. leetcode 题解 Add Two Numbers(两个单链表求和)

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

  9. 【LeetCode每天一题】Add Two Numbers(两链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  10. [leetcode]2. Add Two Numbers两数相加

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

随机推荐

  1. 实现table中checkbox复选框、以及判断checked是否被选中、js操作checkedbox选中

    上图是实现效果. 下面贴代码 表的第一行也就是<th>中的代码,onclick事件是实现全选或者全不选效果. <th> <input id="allboxs&q ...

  2. percentiles of live data capture

    For example, every ten seconds I got a server response time, I want to know the 90 percentile respon ...

  3. 升级到macSierra 10.12之后 在模拟器上面滑动视图很卡,

    解决方法 在终端里面输入sudo sysctl -w kern.timer.coalescing_enabled=0

  4. js检测对象中是否存在某个属性

    1.使用in关键字.该方法可以判断对象的自有属性和继承来的属性是否存在. 2.使用对象的hasOwnProperty()方法.该方法只能判断自有属性是否存在,对于继承属性会返回false. 3.用un ...

  5. xfs文件系统磁盘配额

    引言 这篇文章简单介绍一下xfs文件系统的磁盘配额配置. 文章目录 0×1.开启分区磁盘配额 0×2.使用xfs_quota命令配置磁盘配额 0×1.开启分区磁盘配额 对于ext4文件以前的文件系统, ...

  6. ubuntu14.04 下安装mysql5.6

    1.sudo apt-get install mysql-server-5.6 2.测试是否安装成功 ps aux |grep mysql mysql -u root -p 3.允许远程访问设置 su ...

  7. Linux 相关的error处理

    1  dpkg: error: duplicate file trigger interest for filename Notice the first and last lines of /var ...

  8. bower工具的简单使用

    基于NodeJS的一个静态资源管理工具,由twitter公司开发维,解决大型网站中静态资源的依赖问题. 1.依赖NodeJS环境和git工具. 2.npm install -g bower安装bowe ...

  9. wps使用积累

    1.word加批注: 选中文字--插入--批注

  10. 第一百一十三节,JavaScript文档对象,DOM基础

    JavaScript文档对象,DOM基础 学习要点: 1.DOM介绍 2.查找元素 3.DOM节点 4.节点操作 DOM(Document Object Model)即文档对象模型,针对HTML和XM ...