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 ...
随机推荐
- 关于Idea启动配置tomcat
1.打开file中setting中搜索Application Servers,如下图 2.添加服务器类型,例如tomcat,如下图,添加完成之后可以选定tomcat的目录,tomcat Home配置t ...
- C# 之TripleDESCryptoServiceProvider类加密/解密程序
这篇博文的编写基于以下博客地址提供的知识: TripleDESCryptoServiceProvider 加密解密 基于该博客,我的毕业设计中密码存储加密字符串这一问题得到解决.
- 3.6 html报告乱码问题优化
3.6 html报告乱码问题优化 前言python2用HTMLTestRunner生成测试报告时,有中文输出情况会出现乱码,这个主要是编码格式不统一,改下编码格式就行.下载地址:http://tung ...
- JavaBasic_12
类 抽象:对象的共性 类:产生对象的模板(对于属性来讲,类规定有没有属性以及属性类型,具体的属性值因对象的不同而不同) 数据类型:数据集及基于这个数据集的操作 类:我们自定义类型(定义成员变量,基于数 ...
- UVa839
这个引用好精髓. #include <iostream> #include <cstring> #include <string> #include <map ...
- 软件工程 week 05
关于 石墨文档客户端 的案例分析 作业地址:https://edu.cnblogs.com/campus/nenu/2016CS/homework/2505 一.调研测评 测试平台:Windows 1 ...
- html(二)常见符号
HTML当中的常见实体内容:
- Floodlight1.2+Mininet的安装及使用
直接转载的大神的,亲测Ubuntu16.04版本没问题 https://www.sdnlab.com/19189.html
- [zz]蟑螂蚂蚁蚊子已不住在我家了!这个方法100%见效…
http://mt.sohu.com/20150324/n410238511.shtml 蚂蚁怕酸,蚊子怕辣,蟑螂怕香.在下给各位提供一个不杀生又能驱赶蚂蚁.蚊子.蟑螂的妙法. 一.蚂蚁怕酸味 家里的 ...
- C++类中this指针的理解
先要理解class的意思.class应该理解为一种类型,象int,char一样,是用户自定义的类型.用这个类型可以来声明一个变量,比如int x, myclass my等等.这样就像变量x具有int类 ...