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. php pdo_mysql扩展安装

    本文内容是以 CentOS 为例,红帽系列的 Linux 方法应该都是如此,下面就详细说明步骤,在这里严重鄙视哪些内容??隆⑺档脑悠咴影说挠泄 PDO 编译安装的文章. 1.进入 PHP 的软件包 p ...

  2. python常用函数 D

    defaultdict(set_type) 可以定义字典多值映射,入参类型决定value类型. 例子: deque:(int) 保留最后N个元素 例子: Decimal(float) 直接对浮点数进行 ...

  3. java方法执行流程解析

    Java程序运行时,必须经过编译和运行两个步骤.首先将后缀名为.java的源文件进行编译,最终生成后缀名为.class的字节码文件.然后Java虚拟机将编译好的字节码文件加载到内存(这个过程被称为类加 ...

  4. spark大数据快速分析第二章

    1.驱动程序通过一个SparkContext对象来访问Spark,此对象代表对计算集群的一个连接.shell已经自动创建了一个SparkContext对象.利用SparkContext对象来创建一个R ...

  5. loadRunner函数之lr_set_debug_message

    lr_set_debug_message:选择性开启扩展日志 vuser_init: vuser_init() { ; } Action: Action() { ExtendedLog(); // 开 ...

  6. LUOGU P4088 [USACO18FEB]Slingshot(线段树)

    传送门 解题思路 推了推式子发现是个二维数点,想了想似乎排序加线段树难写,就写了个树套树,结果写完看见空间才\(128M\)..各种奇技淫巧卡空间还是\(MLE\)到天上.后来只好乖乖的写排序+线段树 ...

  7. 【靶场练习_sqli-labs】SQLi-LABS Page-1(Basic Challenges)

    GET篇 Less-1:  1.用order by得出待查表里有三个字段 http://192.168.40.165/sqli-labs-master/Less-1/?id=1' order by 3 ...

  8. 某些 UI效果 实现思路

    一.日历组件: https://blog.csdn.net/amork/article/details/7257212 二.瀑布流 三.轮播图:轮播图已经用的很多了,结构也简单就不去将了. 四.分页组 ...

  9. 在React中跨组件分发状态的三种方法

    在React中跨组件分发状态的三种方法 当我问自己第一百次时,我正在研究一个典型的CRUD屏幕:"我应该将状态保留在这个组件中还是将其移动到父组件?". 如果需要对子组件的状态进行 ...

  10. 判断PC或者是APP

    function isPC() { if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { return fa ...