/**
* 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. 字符串format拼接格式化

    # ###字符串的格式化 format"""(1)顺序传参(2)索引传参(3)关键字传参(4)容器类型传参(列表和元组) {} 相当于占位符""&qu ...

  2. Debian下安装docker

    1.安装docker.io包之前,需要先设置使用backports源 编辑/etc/apt/sources.list文件,加入下面这一句: deb http://http.debian.net/deb ...

  3. 剑指Offer 41. 和为S的连续正数序列 (其他)

    题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...

  4. C#operator作用

    opertor用于定义类型转化时采用两种方式,饮食转换implicit和显示转换explicit public static implicit 目标类型(被转化类型 变量参数) { return 目标 ...

  5. HTML知识点梳理1

    1,HTML基本结构 <!DOCTYPE html> <html> <head></head> <body> </body> & ...

  6. sql语言 含有包含关系的查询 (含mysql 和sql sever)

    一.sql中查询包含关系的查询 sql语句中包含关系可以使用 in 和exist,但有些时候仅仅用这两个是不够的,还有表示方法是  not exist(b expect a )可以表示a包含b. 二. ...

  7. [ Codeforces Round #549 (Div. 2)][D. The Beatles][exgcd]

    https://codeforces.com/contest/1143/problem/D D. The Beatles time limit per test 1 second memory lim ...

  8. 缓慢拒绝服务攻击- slowloris.pl

    How use Slowloris   Requirements: # sudo apt-get update # sudo apt-get install perl # sudo apt-get i ...

  9. Excel中的数据与DataSet的互换

    using System;using System.Collections.Generic;using System.Data;using System.Drawing;using System.IO ...

  10. cmake编译obs

    https://blog.csdn.net/su_vast/article/details/74984213 https://blog.csdn.net/u011258240/article/deta ...