LeetCode Linked List Medium 2. Add Two Numbers
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的更多相关文章
- 【Leetcode】【Medium】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 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 2. 两数相加(Add Two Numbers)
2. 两数相加 2. Add Two Numbers 题目描述 You are given two non-empty linked lists representing two non-negati ...
- (python)leetcode刷题笔记 02 Add Two Numbers
2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...
- leetcode刷题: 002 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 ...
- 【LeetCode每天一题】Add Two Numbers(两链表相加)
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
- 【leetcode刷题笔记】Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode.2-两个数字相加(Add Two Numbers)
这是悦乐书的第340次更新,第364篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第1题(顺位题号是2).给定两个非空链表,表示两个非负整数. 数字以相反的顺序存储, ...
随机推荐
- Envoy的线程模型[翻译]
Envoy threading Model 关于envoy 代码的底层文档相当稀少.为了解决这个问题我计划编写一系列文档来描述各个子系统的工作.由于是第一篇, 请让我知道你希望其他主题覆盖哪些内容. ...
- prometheus 笔记
前言 prometheus 是监控应用软件类似于nagios. 安装 1.官网下载prometheus-2.2.0.linux-amd64压缩包,解压,执行./prometheus即可.这里重要的是配 ...
- hive的数据定义之创建数据库和表
1.对数据库的操作 create database hive_db //创建数据库hive_db create table hive_db.test(字段内容及其格式省略) //在数据库hive_db ...
- VUE中使用canvas做签名功能,兼容IE
<template> <div> <div class="msgInput"> &l ...
- Angular JS - 2 - angularjs helloworld
材料下载 https://github.com/liuch0228/AngularJS-learn.git 1.使用原生jquery实现 实现输入框内容 在页面上跟随输入值动态更新 项目路径 < ...
- kNN(从文本文件中解析数据)
# 准备数据:从文本文件中解析数据# 在kNN.py中创建名为file2matrix的函数,处理输入格式问题# 该函数的输入为文件名字符串,输出为训练样本矩阵和类标签向量# 将文本记录到转换Numpy ...
- window环境下pipd的安装
参照:https://blog.csdn.net/jin80506/article/details/83111848 如果你还是无法使用尝试查看是否自己已经将:C:\software\Python\P ...
- this.$nextTick 与window.setTimeout
两个都可以设置运行先后.前者,方式: this.$nextTick(() => { this.$refs.orgAddOrUpdate.init(row, isAdd) }) 其中orgAddO ...
- [CSP-S模拟测试]:阴阳(容斥+计数+递推)
题目传送门(内部题16) 输入格式 第一行两个整数$n$和$m$,代表网格的大小.接下来$n$行每行一个长度为$m$的字符串,每个字符若为$W$代表这个格子必须为阳,若为$B$代表必须为阴,若为$?$ ...
- python中的Nonetype
在python中的None的类型是Nonetype, 嗯,看清楚了吧,None是值,Nonetype是类型.同理,数字1是值,int是类型.注意:在python中是没有Null的,取而代之的是None