/**
* 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. void的几点用法

    1.可以通过void 0 获取undefined.等同于void(0). void 任意数 === undefined   // true void(0) === undefined  // true ...

  2. mysql 分组排序取最值

    查各个用户下单最早的一条记录 查各个用户下单最早的前两条记录 查各个用户第二次下单的记录 一.建表填数据: SET NAMES utf8mb4; -- 取消外键约束 ; -- ------------ ...

  3. WebView性能、体验分析与优化

    育新 徐宏 嘉洁 ·2017-06-09 20:03 在App开发中,内嵌WebView始终占有着一席之地.它能以较低的成本实现Android.iOS和Web的复用,也可以冠冕堂皇的突破苹果对热更新的 ...

  4. input做一个开关按钮

    .mui-switch { width: 52px; height: 31px; position: relative; border: 1px solid #dfdfdf; background-c ...

  5. 新建虚拟机并与XShell连接(配置网卡)

    新建虚拟机并与XShell连接 一.新建虚拟机 下一步 下一步 选择CentOS版本,我的CentOS镜像是6.5版本64位,所以我选择CentOS 6 64. 然后点击下一步 可以自行更改位置,然后 ...

  6. 神州数码广域网Frame-Relay封装配置(即帧中继)

    实验要求:了解帧中继的配置方法 拓扑如下 R1 enable 进入特权模式 config 进入全局模式 hostname R1 修改名称 interface s0/1 进入端口 ip address ...

  7. php字符串统计次数的各种方法(转)

    <?php $str = 'AbCdEfGaBcDeFgH0234;,!-AaBbCcDdEeFfGg'; $str = strtoupper($str); // 不区分大小写时,全部转换成大写 ...

  8. Python全栈之路----常用模块----sys模块

    sys.argv  命令行参数 List,第一个元素是程序本身路径 #test.py import sys print(sys.argv) D:\ProgramLearning\Py_program& ...

  9. build to win读后感

    在软件开发的过程中,不能盲目去show自己的成果,而是要大量考虑别人的意见,在广范围的撒网之后,收集意见,最后在一锤定音. 还有就是,要懂得团队合作,例如,本文介绍了一个事例,作者的团队与科研团队合作 ...

  10. Appium介绍及工作原理

    一.Appium介绍 Appium是一个开源.跨平台的测试框架,可以用来测试原生及混合的移动端应用.Appium支持IOS.Android及FirefoxOS平台.Appium使用WebDriver的 ...