/**
* 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的更多相关文章

  1. 【LeetCode2】Add Two Numbers★★

    题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(2->4->3)(342) + (5-> ...

  2. Leetcode-2 Add Two Numbers

    #2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits ...

  3. Leetcode2:Add Two Numbers@Python

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 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 ...

  5. leetcode2:Add Two Numbers

    Add Two Numbers Total Accepted: 55216 Total Submissions: 249950My Submissions You are given two link ...

  6. 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], ...

  7. leetcode-2 Add Two Numbers 计算两个对应的列表和问题

     1.问题描写叙述: You are given two linked lists representing two non-negativenumbers. The digits are sto ...

  8. LeetCode-2 Keys Keyboard

    package Classify.DP.Medium; import org.junit.jupiter.api.Test; /** Initially on a notepad only one c ...

  9. [Swift]LeetCode2. 两数相加 | Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

随机推荐

  1. Beta版——爱阅APP功能说明书

    爱阅APP功能说明书 一.引言 通过Alpha发布和一些用户的反馈信息,了解到我们APP存在的问题.针对这些问题我们做了一些修改.以下内容是Beta版的功能说明书. 二.工具 安卓手机 爱阅APP安装 ...

  2. brand new start

    做了约两年半的安全,留下了约五十多篇笔记,从电脑搬过来,免的丢了

  3. angular2 ng2-validation 表单验证

    1:安装模块 npm install ng2-validation --save 2:配置app.module.ts import { FormsModule, <font color=&quo ...

  4. 如何利用 Git 与 GitHub 进行多人协作开发

    方法一:添加 Collaborators Collaborators 类似于Team模式. Repository的拥有者Owner 可以直接添加合作者到自己的仓库中, 让合作者拥有几乎等同拥有者的权限 ...

  5. 软件测试_Linux

    # Linux## 基础知识### 操作系统* 作为中间人,连接软件和硬件### Linux * 特点 * 免费+安全### 查看日志,定位bug,修改文件,搭建环境## 安装### 装虚拟机 vmw ...

  6. 《从Lucene到Elasticsearch:全文检索实战》学习笔记四

    今天我给大家讲讲布尔检索模型基本概念 布尔检索模型: 检索模型是判断文档内容与用户相关性的核心技术,以大规模网页搜索为例,在海量网页中与用户查询关键词相关的网页可能会有成千上万个,甚至耕读哦.那么信息 ...

  7. Linux上的文件管理类命令(2)

    Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示: wc 统计文件中的字节数.单词数.行数.并将统计结果显示输出 -c, --bytes 打印字节数 [root@qingchen ...

  8. form表单以及内嵌框架标签

    今关于今天所学习的东西又复杂又简单,上午学习了form表单,也是挺简单的:搭配table表格使用也是非常熟练. 下午讲了讲给网页内嵌框架标签以及在内嵌框架标签中添加其他的网页:还有在内嵌框架标签中添加 ...

  9. Google - chanceToLose24Game

    /* 一个类似24点的游戏,假设牌桌上有无数张1-10的牌,然后你手上的牌的总和是k,现在你可以随机到牌桌上抽牌加到总和里,如果你手上牌的总和在20-25之间就是win,如果总和超过25就是lose, ...

  10. EBS获取code_combination_id(CCID)时段值自动被置为默认值的问题

    EBS中在使用标准的API(FND_FLEX_EXT.GET_COMBINATION_ID 和 FND_FLEX_EXT.GET_CCID还有fnd_flex_keyval.validate_segs ...