LeetCode--No.002 Add Two Numbers
Add Two Numbers
- Total Accepted: 160702
- Total Submissions: 664770
- Difficulty: Medium
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 Num2 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null){
return l2 ;
}
if(l2 == null){
return l1 ;
}
int nextBit = (l1.val + l2.val)/10 ;
int curBit = (l1.val + l2.val)%10 ;
ListNode head = new ListNode(curBit) ;
ListNode temp = head ;
l1 = l1.next ;
l2 = l2.next ;
//当l1、l2对应位均存在时,进行计算
while(l1 != null && l2 != null){
curBit = (l1.val + l2.val + nextBit) % 10 ;
nextBit = (l1.val + l2.val + nextBit)/10 ;
ListNode node = new ListNode(curBit) ;
temp.next = node ;
temp = temp.next ;
l1 = l1.next ;
l2 = l2.next ;
}
//判断l1是否结束,没有结束继续
while(l1 != null){
curBit = (l1.val + nextBit) % 10 ;
nextBit = (l1.val + nextBit)/10 ;
ListNode node = new ListNode(curBit) ;
temp.next = node ;
temp = temp.next ;
l1 = l1.next ;
}
//判断l2是否结束,没有结束继续
while(l2 != null){
curBit = (l2.val + nextBit) % 10 ;
nextBit = (l2.val + nextBit)/10 ;
ListNode node = new ListNode(curBit) ;
temp.next = node ;
temp = temp.next ;
l2 = l2.next ;
}
//判断最后的进位位是否为0 ,不为0则需要保存下一位
if(nextBit != 0){
ListNode node = new ListNode(nextBit) ;
temp.next = node ;
}
return head ;
}
}
LeetCode--No.002 Add Two Numbers的更多相关文章
- 【LeetCode】002 Add Two Numbers
		题目: You are given two non-empty linked lists representing two non-negative integers. The digits are ... 
- leetcode 第二题Add Two Numbers  java
		链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ... 
- LeetCode #002# Add Two Numbers(js描述)
		索引 思路1:基本加法规则 思路2:移花接木法... 问题描述:https://leetcode.com/problems/add-two-numbers/ 思路1:基本加法规则 根据小学学的基本加法 ... 
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ... 
- No.002 Add Two Numbers
		Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ... 
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
		我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ... 
- C# 写 LeetCode Medium #2 Add Two Numbers
		2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ... 
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
		题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ... 
- leetcode刷题: 002 Add Two Numbers
		You are given two linked lists representing two non-negative numbers. The digits are stored in rever ... 
随机推荐
- 网络抓包工具 wireshark 入门教程
			Wireshark Wireshark(前称Ethereal)是一个网络数据包分析软件.网络数据包分析软件的功能是截取网络数据包,并尽可能显示出最为详细的网络数据包数据.Wireshark使用WinP ... 
- Javascript的算法题目
			用js实现单链表的增删,直接上代码 const linkList=new LinkList() function LinkList(){ var Node=function(element){ thi ... 
- Linux yum源配置
			Linux yum源配置 本文介绍Red Hat下yum源配置方法,Redhat使用yum网络源需要购买服务,但是本地yum源不会收费. CentOS用户自带yum源,并且yum不收费. 准备工具: ... 
- Vue框架H5商城类项目商品详情点击返回弹出推荐商品弹窗的实现方案
			需求场景: 非推荐商品详情页返回的时候弹出弹窗推荐商品,点击弹窗按钮可以直接访问推荐商品: 只有直接进入商品详情页返回才会弹出推荐商品弹窗: 每个用户访问只能弹一次(除非清除缓存). 需求分析: 1. ... 
- java  线程Thread 技术--volatile关键字
			java 语言中允许线程访问共享变量,为了保证共享变量能被准确和一致的更新,Java 语言提供了volatile 关键字,也就是我们所说的内存一致性: 问题抛出:(尝试去运行下面代码,以及将volat ... 
- php设计模式-工厂模式(一)
			<?php abstract class Creator{ /* startFactory 返回一个具体的产品 factoryMethod 返回对象 */ protected abstract ... 
- 循序渐进VBA EXCEL数据操作小实例
			1 向指定单元格区域内写入数据 Sub example1() ) arr() = Array("A", "B", "C", "D& ... 
- vscode快捷键的中文版
			自己整理了一份vscode快捷键的中文版本 
- zeromq学习记录(五)vc下多线程
			/************************************************************** 技术博客 http://www.cnblogs.com/itdef/ ... 
- zeromq学习记录(一)最初的简单示例使用ZMQ_REQ ZMQ_REP
			阅读zeromq guide的一些学习记录 zeromq官方例子 在VC下运行会有些跨平台的错误 我这里有做修改 稍后会发布出来 相关的代码与库 http://download.zeromq.org ... 
