Description


You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: ( ->  -> ) + ( ->  -> )
Output: -> ->
Explanation: + = .

题目描述:两个非空链表,代表两个非负整数。链表从尾部到头部每一个元素代表非负整数的每一位的值。技术这两个数的值,并把结果放到一个链表中。

我的思路:链表长度不限,所以不能转成int 或者long 进行计算。同时 每一位进行计算的时候,会有进位产生。下面是我的解法

/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
ListNode res = new ListNode();
ListNode temp = res;
bool flag = false;
int sum = ;
while(l1 != null && l2 != null){
temp.next = new ListNode((l1.val + l2.val + sum)%);
sum = (l1.val + l2.val + sum)/;
l1 = l1.next;
l2 = l2.next;
temp = temp.next; }
while(l1 !=null){
temp.next = new ListNode((l1.val + sum)%);
sum = (l1.val+sum)/;
l1 = l1.next;
temp = temp.next;
}
while(l2 !=null){
temp.next = new ListNode((l2.val + sum)%);
sum = (l2.val+sum)/;
l2 = l2.next;
temp = temp.next;
}
if(sum != )
temp.next=new ListNode();
return res.next;
}
}

LeetCode Linked List Medium 2. Add Two Numbers的更多相关文章

  1. 【Leetcode】【Medium】Add Two Numbers

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

  2. C# 写 LeetCode Medium #2 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

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

    2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...

  4. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  5. leetcode刷题: 002 Add Two Numbers

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

  6. LeetCode第四题,Add Two Numbers

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

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

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

  8. 【leetcode刷题笔记】Add Two Numbers

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

  9. LeetCode.2-两个数字相加(Add Two Numbers)

    这是悦乐书的第340次更新,第364篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第1题(顺位题号是2).给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储, ...

随机推荐

  1. Kintex7 XC7K325T 板卡三剑客

    (226)基于Xilinx Kintex-7 FPGA K7 XC7K325T PCIeX8 四路光纤卡   (227)基于Xilinx Kintex-7 FPGA K7 XC7K325T的FMC U ...

  2. 137-基于TMS320C6678、FPGA XC5VSX95T的四路Base模式全景影像处理平台

    基于TMS320C6678.FPGA XC5VSX95T的四路Base模式全景影像处理平台 一.板卡概述 本板卡由我公司自主研发,基于CPCI架构,符合CPCI2.0标准,采用两片TI DSP TMS ...

  3. css3 清除浮动

    eg:三个div,父级div下面有两个div分别float:left和float:right <style> .container{width:400px;border:3px soild ...

  4. 【抓包工具之Fiddler】增加IP列;session高亮

    Fiddler 在处理每个session时,脚本文件CustomRules.js中的方法都会运行,该脚本使得你可以隐藏,标识或任意修改负责的session.规则脚本在运行状态下就可以修改并重新编译,不 ...

  5. fork/join并发编程

    Fork & Join 的具体含义 Fork 一词的原始含义是吃饭用的叉子,也有分叉的意思.在Linux 平台中,函数 fork()用来创建子进程,使得系统进程可以多一个执行分支.在 Java ...

  6. JVM参数说明

    转载于https://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html文章 JVM参数说明 -Xms:初始堆大小  默认值=物理内存的1 ...

  7. web项目使用fastdsf上传|下载文件

    在上传代码中添加一下代码 suffix=suffix.substring(1); fast.FastDFSFile file = new fast.FastDFSFile(mFile.getBytes ...

  8. Django前后端分离跨域请求问题

    一.问题背景 之前使用django+vue进行前后端分离碰到跨域请求问题,跨域(域名或者端口不同)请求问题的本质是由于浏览器的同源策略导致的,当请求的响应不是处于同一个域名和端口下,浏览器不会接受响应 ...

  9. 【串线篇】Mybatis之动态sql

    一.if标签 <select id="getTeacherByCondition" resultMap="teacherMap"> select * ...

  10. BZOJ4671 异或图 斯特林反演+线性基

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4671 题解 半年前刚学计数的时候对这道题怀着深深的景仰,现在终于可以来做这道题了. 类似于一般 ...