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. 编译错误:warning C4005]ws2def.h(91): warning C4005: “AF_IPX”: 宏重定义 winsock.h(460) : 参见“AF_IPX”的前一个定义

    [问题] ws2def.h(91): warning C4005: “AF_IPX”: 宏重定义: winsock2.h(460) : 参见“AF_IPX”的前一个定义 [原因] windows.h头 ...

  2. CSAW CTF Qualification Round 2018 - shell->code

    原题 Linked lists are great! They let you chain pieces of data together. nc pwn.chal.csaw.io 9005 链接:h ...

  3. jenkins持续集成(三): jenkins配置邮件通知

    完成基于jenkins的持续集成部署后,任务构建执行完成,测试结果需要通知到相关人员.这篇博客,介绍如何在jenkins中配置邮件通知的方法... 一.安装邮件插件 由于Jenkins自带的邮件功能比 ...

  4. elasticsearch相关聚合查询示例

    索引(index):logstash-nginx-*,type:nginx_access 请求路径: 1.按照某个字段进行分组统计访问量 { "query": { "bo ...

  5. HTML5 canvas绘制图形

    demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  6. centos 6.5 查看 IP

    ip 和 ifconfig 两个命令都可以,但现在推荐使用 ip ip addr ifconfig

  7. vue 全局 js 方法

    1.新增 getCurrentDataType.js 文件 import cookieUtils from '@/config/cookieUtils' function getCurrentData ...

  8. mybatis框架之装饰模式

    学习开源框架源码,除了储备点知识以便于与面试官互相忽略之外,我想最重要的还是去学习大神如何写代码,如何做到职责单一,如何做到可扩展等... 本篇,试着总结一下mybatis在缓存模块使用到的装饰模式. ...

  9. JS中关于引用类型数据及函数的参数传递

    (JavaScript 中,函数的参数传递方式都是按值传递,没有按引用传递的参数) 一.数据类型 在 javascript 中数据类型可以分为两类: 基本类型值 primitive type,比如Un ...

  10. keil c51 不能使用:Go to Definition of....的解决方法 STC51

    keil c51 不能使用:Go to Definition of....的解决方法 达到的目标如下图所示: 解决方法为 :在工程栏右键单击进入Manage Components ,然后点确定,前提是 ...