Leetcode:Add Two Numbers分析和实现
Add Two Numbers这个问题的意思是,提供两条链表,每条链表表示一个十进制整数,其每一位对应链表的一个结点。比如345表示为链表5->4->3。而我们需要做的就是将两条链表代表的整数加起来,并创建一个新的链表并返回,新链表代表加总值。
这个问题与算法无关,其主要是操作链表这一数据结构,以及两个大数的加法。由于没有规定链表的长度,这意味着链表代表的整数可以任意大,这样就不能先利用链表计算出对应的整数值,之后利用加总值重新生成链表。我们必须手动处理两个大数的加法。
要谨慎两个个位数的加总结果是有可能需要进位的,即7+8实际上等于10x1+5,其中1应该进位到下一个结点,而5当作这个当前结点的值。
addTwoNumbers(Node n1, Node n2)
res = Node{val = (n1.val + n2.val) % 10, next = null}
tail = res
adv = (n1.val + n2.val) / 10
n1 = n1.next
n2 = n2.next
while n1 != null && n2 != null then
sum = n1.val + n2.val + adv
tail.next = Node{val = sum % 10, next = null}
tail = tail.next
adv = sum / 10
n1 = n1.next
n2 = n2.next
Node notEndedNode = (n1 == null ? n2 : n1)
while notEndedNode != null then
sum = notEndedNode .val + adv
tail.next = Node{val = sum % 10, next = null}
tail = tail.next
adv = sum / 10
notEndedNode = notEndedNode.next
while adv != 0 then
tail.next = Node{val = adv % 10, next = null}
tail = tail.next
adv = adv / 10
return res
下面直接给出java的实现代码
package cn.dalt.leetcode;
/**
* Created by Administrator on 2017/6/4.
*/
public class AddTwoNumbers {
public static void main(String[] args)
{
System.out.println(new AddTwoNumbers().addTwoNumbers(new ListNode(342), new ListNode(465)));
}
static class ListNode {
ListNode(int x)
{
val = x % 10;
int adv = x / 10;
if(adv != 0)
{
next = new ListNode(adv);
}
}
@Override
public String toString() {
return "" + val + (next != null ? "->" + next.toString() : "");
}
int val;
ListNode next;
}
static class NodeList{
ListNode tail;
ListNode root;
int advance = 0;
public NodeList(int x)
{
root = new ListNode(x % 10);
tail = root;
advance = x / 10;
}
public void append(int x)
{
x += advance;
ListNode temp = new ListNode(x % 10);
advance = x / 10;
tail.next = temp;
tail = temp;
}
public ListNode getResult()
{
while(advance != 0)
{
append(0);
}
return root;
}
@Override
public String toString() {
return root.toString();
}
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
NodeList list = new NodeList(l1.val + l2.val);
l1 = l1.next;
l2 = l2.next;
while(l1 != null && l2 != null)
{
list.append(l1.val + l2.val);
l1 = l1.next;
l2 = l2.next;
}
while(l1 != null)
{
list.append(l1.val);
l1 = l1.next;
}
while(l2 != null)
{
list.append(l2.val);
l2 = l2.next;
}
return list.getResult();
}
}
Leetcode:Add Two Numbers分析和实现的更多相关文章
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- LeetCode: Add Two Numbers 解题报告
Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are ...
- [LeetCode] Add Two Numbers题解
Add Two Numbers: You are given two non-empty linked lists representing two non-negative integers. Th ...
- [Leetcode] Add two numbers 两数之和
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode——Add Two Numbers
Question:You are given two linked lists representing two non-negative numbers. The digits are stored ...
- [LeetCode] Add Two Numbers 链表
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
随机推荐
- Custom Ribbon in SharePoint 2010 & which not wrok when migrate from 2010 to 2013
博客地址 http://blog.csdn.net/foxdave 1. First of all, let me show you the ribbon modal in our project w ...
- 创建一个新的进程os.fork
import os pid = os.fork()功能:创建新的进程参数:无返回值:失败返回一个负数 成功:在原有进程中返回一个新的进程的PID号 在新的进程中返回0 *子进程会复制父进程全部代码段, ...
- Python 使用Microsoft SQL Server数据库
软件环境: Windows 7 32bit Python 3.6 Download https://www.python.org/downloads/ 默认安装,并添加环境变量,一路Next ... ...
- Java进行数据库导出导入 亲测可用
/** * @param hostIP ip地址,可以是本机也可以是远程 * @param userName 数据库的用户名 * @param password 数据库的密码 * @param sav ...
- html页面控制字体大小的js代码
dom对象控制显示文章字体大小的js代码 <head> <script type="text/javascript"> function check(siz ...
- leetcode:Insert Sort List
问题描写叙述 对一个单链表进行插入排序,head指向第一个结点. 代码 /** * Definition for singly-linked list. * struct ListNode { * i ...
- WinForm 弹窗
private void FrmMyShow_Load(object sender, EventArgs e) { Rectangle r = Screen.GetWorkingArea(this); ...
- 异步FIFO中空满信号如何产生?
异步FIFO中,空满信号该如何产生呢? 在复位的时候,读指针和写指针相等,读空信号有效(这里所说的指针其实就是读地址.写地址)当读指针赶上写指针的时候,写指针等于读指针意味着最后一个数据被读完,此时读 ...
- C++ 类外定义
类内定义与内联函数 像下面这种函数名与函数体都写在类里面的函数形式被称作类内定义,编译器编译的时候会把它默认成内联函数. class Student { public: void setAge(int ...
- 配置spring的log4j日志记录
1.导入依赖包pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...