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 ...
随机推荐
- 带通滤波 matlab
巴特沃斯:1.带阻滤波器设计带阻滤波器指标:阻带上边界频率:5Kz:阻带下边界频率:7Kz:通带上边界频率:2Kz:通带下边界频率:9Kz:通带最大衰减:1dB:阻带最小衰减:20dB:设计程序如下: ...
- python:3种爬虫的优缺点
性能对比 爬取方法 性 能 ...
- 关于$\mathcal{D}(0,1)$上的一个有趣结论
[转载请注明出处]http://www.cnblogs.com/mashiqi 2017/02/20 在$\mathcal{D}(0,1)$上取定$\varphi_0 \in \mathcal{D}( ...
- C#operator作用
opertor用于定义类型转化时采用两种方式,饮食转换implicit和显示转换explicit public static implicit 目标类型(被转化类型 变量参数) { return 目标 ...
- ckeditor_学习(1) 基本使用
ckeditor 是一款强大的web编辑器.工作需要用到记录学习和使用过程,版本是ckeditor4. 1.下载ckeditor的安装包,建议下载标准版的. j将ckeditor.js 引入页面,调用 ...
- Centos 安装 python2.7.10以及pip
安装python2.7.10 1. 下载安装包并解压 wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz tar -xf P ...
- 通过父元素的hover控制子元素的显示
.searbar_content_box:hover .searchBar_checked_detail_box{ display:block}
- Java实现带logo的二维码
Java实现带logo的二维码 二维码应用到生活的各个方面,会用代码实现二维码,我想一定是一项加分的技能.好了,我们来一起实现一下吧. 我们实现的二维码是基于QR Code的标准的,QR Code是由 ...
- 从npm 角度理解 mvn 的 pom.xml
从npm 角度理解 mvn 的 pom.xml pom -- project object model. 用于描述项目的配置: 基础说明 依赖 如何构建运行 类似 node.js 的 package. ...
- Dynamics CRM Publisher
如果你想创建并且部署一个solution(解决方案),你需要创建一个publisher. 当准备创建一个solution并且创建entity field, relationship在这个solutio ...