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. anglarjs概述

    anglarjs 是一个MVC框架,是开发单页面应用的上上之选,它不是一个功能库,是一个开发动态页面的html框架.专注于扩展html功能,提供动态数据绑定,并能够与jquery合作融洽. 要定义一个 ...

  2. JAVA MONGODB group查询的UTC时间问题

    BasicDBList dateList = new BasicDBList(); dateList.add("$t"); dateList.add(28800000); DBOb ...

  3. TheSixthWeekJavaText

    加密文档 实验要求编写一个算法加密一串英文字串. 设计思想:对于一个字符串,我们可以用String.charAt()方法依次取出其中的字符元素,组成一个字符数组.由于字符可以转化为short类型变量进 ...

  4. sql优化方式-转载

    我始终认为,一个系统的性能的提高,不单单是试运行或者维护阶段的性能调优的任务,也不单单是开发阶段的事情,而是在整个软件生命周期都需要注意,进行有效工作才能达到的.所以我希望按照软件生命周期的不同阶段来 ...

  5. 使用vs2010打开VS2013的工程文件

    在开发团队,会出现vs工具使用版本的不一样的情况.我的电脑使用的是VS2010,可是其他人员使用的是vs2013. 要打开其他人员上传的工程文件,可以通过三种方式: 1.下载一个vs2013版本. 2 ...

  6. ubuntu jdk环境变量配置

    export JAVA_HOME=/usr/local/java/jdk1.8.0_25  export JRE_HOME=${JAVA_HOME}/jre  export CLASSPATH=.:$ ...

  7. jquery正则常用的

    jQuery.validator.addMethod("mobilePhone",function(value,element){ return this.optional(ele ...

  8. File对象的常用方法

    File对象不仅可以表示文件,还可以表示目录,源码注释是这么说的:An abstract representation of file and directory pathnames. File类最常 ...

  9. CodeForces 705A Hulk

    水题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #includ ...

  10. SVD分解技术数学解释

    SVD分解 SVD分解是LSA的数学基础,本文是我的LSA学习笔记的一部分,之所以单独拿出来,是因为SVD可以说是LSA的基础,要理解LSA必须了解SVD,因此将LSA笔记的SVD一节单独作为一篇文章 ...