leetcode2
/**
* 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) {
var list = new List<ListNode>();
var temp1 = l1;
var temp2 = l2;
var step = ;
while (l1 != null || l2 != null)
{
var v1 = ;
var v2 = ;
if (l1 == null)
{
l1 = new ListNode();
}
v1 = l1.val;
l1 = l1.next; if (l2 == null)
{
l2 = new ListNode();
}
v2 = l2.val;
l2 = l2.next; var cur = v1 + v2 + step;
var result = ;
if (cur >= )
{
step = ;
result = cur % ;
}
else
{
step = ;
result = cur;
}
list.Add(new ListNode(result));
}
if (step == )
{
list.Add(new ListNode());
} for (int i = ; i < list.Count - ; i++)
{
list[i].next = list[i + ];
}
var head = list[];
return head;
}
}
https://leetcode.com/problems/add-two-numbers/#/description
补充python实现:
class Solution:
def addTwoNumbers(self, l1: 'ListNode', l2: 'ListNode') -> 'ListNode':
headnode = ListNode(0)
temp = headnode
carry = 0
while l1!=None or l2!=None:
if l1==None:
l1 = ListNode(0)
if l2==None:
l2 = ListNode(0)
cur = l1.val + l2.val + carry
if cur // 10 > 0:
carry = 1
else:
carry = 0
cur = cur % 10
temp.next = ListNode(cur)
temp = temp.next
l1 = l1.next
l2 = l2.next
if carry > 0:
temp.next = ListNode(carry)
return headnode.next
leetcode2的更多相关文章
- 【LeetCode2】Add Two Numbers★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(2->4->3)(342) + (5-> ...
- Leetcode-2 Add Two Numbers
#2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...
- Leetcode2:Add Two Numbers@Python
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- LeetCode2:Median of Two Sorted Arrays
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- leetcode2:Add Two Numbers
Add Two Numbers Total Accepted: 55216 Total Submissions: 249950My Submissions You are given two link ...
- leetcode2 Two Sum II – Input array is sorted
Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...
- leetcode-2 Add Two Numbers 计算两个对应的列表和问题
1.问题描写叙述: You are given two linked lists representing two non-negativenumbers. The digits are sto ...
- LeetCode-2 Keys Keyboard
package Classify.DP.Medium; import org.junit.jupiter.api.Test; /** Initially on a notepad only one c ...
- [Swift]LeetCode2. 两数相加 | Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
随机推荐
- python2入门(2)
四.python条件语句 if语句基本语法if 判断条件: 执行语句块else if: 执行语句块else: 执行语句 五.循环语句 1 - while循环基本语法while 判断条件: 执行语句块w ...
- Socket网络编程入门
Socket:专业术语:套接字;通俗的解释:两孔插座(一个孔:IP地址,一个孔:端口号).使用场景:通信,如QQ好友交谈,如浏览器的进程怎么与web服务器通信等. Socket来历: socket起源 ...
- scrapy框架的日志等级和请求传参
日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息 ...
- Zookeeper之入门(原理、基础知识)
Zookeeper介绍 Zookeeper是分布式应用程序的协调服务框架,是Hadoop的重要组件.ZK要解决的问题: 1.分布式环境下的数据一致性. 2.分布式环境下的统一命名服务 3.分布式环境下 ...
- spring(三、spring中的eheche缓存、redis使用)
spring(三.spring中的eheche缓存.redis使用) 本文主要介绍为什么要构建ehcache+redis两级缓存?以及在实战中如何实现?思考如何配置缓存策略更合适?这样的方案可能遗留什 ...
- net core 解除上传大附件的限制
1.对于使用自带服务器kernal的情况下,只需要在对应的action上添加属性 DisableRequestSizeLimit [HttpPost] [DisableRequestSizeLimit ...
- sql server中case when的用法
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...
- day06python 哈希 字典集合嵌套
1.hash算法 hash算法 :内存中将值进行hash算法得到一个数值存储在内存中,查找也会按照算法进行查找,使用hash算法 执行效率高相对于list的索引查找 (字典,集合):使用的是hash查 ...
- python: ImportError:DLL load failed 解决方法。
在学习使用wordcloud 库创建词云过程中,mooc里提到可以使用另一个库函数,来创建不同形状的词云. 就是这句: ... from scipy.misc import imread mk = i ...
- mac添加redis 环境变量
cd /etc/paths.d touch redis vim redis 写入 /Users/love/Downloads/redis-4.0.10/src 之后就可以直接执行redis-cli r ...