题目描述:

方法一:

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
cur_l1 = l1
cur_l2 = l2
cur = ListNode(0)
head = cur
flag = 0
while cur_l1 != None or cur_l2 != None:
value = 0
value += flag
if cur_l1 != None:
value += cur_l1.val
if cur_l2 != None:
value += cur_l2.val
new_node = ListNode(0)
cur.next = new_node
cur = cur.next
cur.val = value % 10
flag = value // 10
if cur_l1 != None:
cur_l1 = cur_l1.next
if cur_l2 != None:
cur_l2 = cur_l2.next if flag != 0:
new_node = ListNode(0)
cur.next = new_node
cur = cur.next
cur.val = flag
return head.next

leetcood学习笔记-2-两数相加的更多相关文章

  1. leetcood学习笔记-202-快乐数

    题目描述: 方法一:比较笨的办法,根据题意,如果变成1返回True,如果出现重复返回False 看到下面有位朋友用的是dict,我用了list,两个都跑了一下似乎list快一点? class Solu ...

  2. 两数之和,两数相加(leetcode)

    我们都知道算法是程序员成长重要的一环,怎么才能提高算法呢, 出来在网上看视频之外,动手练习是非常重要的.leetcode 就是一个非常好的锻炼平台. 1. 两数之和,在 leetcode 里面是属于 ...

  3. LeetCode(2):Add Two Numbers 两数相加

    Medium! 题目描述: 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头 ...

  4. leetcode刷题2:两数相加add_two_numbers

    题目:两数相加 (难度:中等) 给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字. 将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以 ...

  5. Leetcode(2)两数相加

    Leetcode(2)两数相加 [题目表述]: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两 ...

  6. C语言链表之两数相加

    题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  7. 【LeetCode每日一题 Day 2】2. 两数相加

    大家好,我是编程熊,今天是LeetCode每日一题的第二天,一起学习的是LeetCode第二题<两数相加>. 题意 给你两个 非空 的链表,表示两个非负的整数.它们每位数字都是按照 逆序 ...

  8. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  9. ✡ leetcode 167. Two Sum II - Input array is sorted 求两数相加等于一个数的位置 --------- java

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

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

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

随机推荐

  1. Spring框架4大原则和主要功能

    Spring框架4大原则: 使用POJO进行轻量级和最小侵入式开发 POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJ ...

  2. echats问题

    echats 横轴显示不下datazoom配置,加入滚动条 实例博客  https://blog.csdn.net/Zheng_xiao_xin/article/details/80882113 常用 ...

  3. shell 例子

    shell编程入门 http://www.runoob.com/linux/linux-shell-variable.html http://c.biancheng.net/cpp/shell/ .查 ...

  4. ajax-jq

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. nacos配置服务入门

    1.nacos服务端部署 参见官方文档:https://nacos.io/zh-cn/docs/quick-start.html 2.nacos配置中心功能使用 在pol文件中添加依赖: 在启动类中使 ...

  6. BlockingQueu 阻塞队列

    java.util.concurrent public interface BlockingQueue<E> extends Queue<E> 简介 当阻塞队列插入数据时: 如 ...

  7. delphi xe10 网络连接

    //当前网络状态(引用 Androidapi.JNI.Network.pas) IsConnected //连接 IsWiFiConnected //Wifi是否连接 IsMobileConnecte ...

  8. bzoj1002题解

    [题意分析] 给你一张特殊的,被称为“轮状基”的无向图,求其生成树个数. [解题思路] 引理: 基尔霍夫矩阵: 基尔霍夫矩阵=度数矩阵-邻接矩阵(邻接矩阵权=两点连边数) Matrix-Tree定理: ...

  9. jar中没有主清单属性【解决办法】

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compi ...

  10. 7 Scatter-loading Features

    7.1 About scatter-loading The scatter-loading mechanism enables you to specify the memory map of an ...