题目描述:

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)

Output: 7 -> 0 -> 8

解题思路:

链表操作

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# @return a ListNode
def addTwoNumbers(self, l1, l2):
l = ListNode(-1)
lt = l
jin = 0
while l1 != None or l2 != None:
if l1 == None:
l1 = ListNode(0)
if l2 == None:
l2 = ListNode(0)
if l.val == -1:
t = l1.val + l2.val
if t > 9:
jin = 1
lt.val = t - 10
else:
lt.val = t
else:
t = l1.val + l2.val + jin
print t
jin = 0
if t > 9:
jin = 1
lt.next = ListNode(t - 10) else:
lt.next = ListNode(t)
lt = lt.next
l1 = l1.next
l2 = l2.next
if jin == 1:
print jin
lt.next = ListNode(1)
return l

【leetcode】Add Two Numbers的更多相关文章

  1. 【题解】【链表】【Leetcode】Add Two Numbers

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

  2. 【leetcode】Add Two Numbers(middle) ☆

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

  3. 【leetcode】 Add Two Numbers

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

  4. 【LeetCode】Add Two Numbers(两数相加)

    这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...

  5. 【LeetCode445】 Add Two Numbers II★★

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

  6. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  7. 【Leetcode】【Medium】Add Two Numbers

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

  8. 【链表】Add Two Numbers

    题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...

  9. 【leetcode】1215.Stepping Numbers

    题目如下: A Stepping Number is an integer such that all of its adjacent digits have an absolute differen ...

随机推荐

  1. 【转】[Intel/Nvidia]Ubuntu 16.04 LTS Intel/Nvidia双显卡切换

    1.在Unity中搜索 "Additional Drivers" 2.打开并选择以下选项 3.打开终端并输入 sudo apt-get install nvidia-361 4.安 ...

  2. jq使用技巧

    1. 如何创建嵌套的过滤器 //允许你减少集合中的匹配元素的过滤器,  //只剩下那些与给定的选择器匹配的部分.在这种情况下,  //查询删除了任何没(:not)有(:has)  //包含class为 ...

  3. vue自定义指令

    Vue自定义指令: Vue.directive('myDr', function (el, binding) { el.onclick =function(){ binding.value(); } ...

  4. github免密码设置

    在创建好了github账号之后,我们可以新建自己的github项目.然而,我们在本地代码升级维护的过程中,涉及到git操作的时候并不是想每次都重新输入密码.这个时候我们需要使用ssh和私钥(公钥)来方 ...

  5. NSSearchPathForDirectoriesInDomains函数详解

    NSSearchPathForDirectoriesInDomains函数详解     #import "NSString+FilePath.h" @implementation ...

  6. Workflow 中做拒绝操作时强制输入拒绝信息

    在做AP发票审批驳回时,客户要求必须强制输入拒绝原因,代码如下: PROCEDURE Validate_Response ( Itemtype IN VARCHAR2, Itemkey IN VARC ...

  7. href="javascript:;" 作用

    <a href="javascript:;" onclick="doExport(this)" class="easyui-linkbutton ...

  8. 各大浏览器内核介绍(Rendering Engine)

    在介绍各大浏览器的内核之前,我们先来了解一下什么是浏览器内核. 所谓浏览器内核就是指浏览器最重要或者说核心的部分"Rendering Engine",译为"渲染引擎&qu ...

  9. protobuf编译出错的解决方案(iOS,OSX)

    protobuf 最近使用protobuf,变编译工具时遇上一点问题.现在附上解决方案 编译过程 完全参照 https://github.com/alexeyxo/protobuf-objc 编译出错 ...

  10. [COJ2201][KOJ0223][KOJ0250]花园

    [KOJ0223][KOJ0250]花园 试题描述 小奇的花园有n个温室,标号为1到n,温室以及以及温室间的双向道路形成一棵树. 每个温室都种植着一种花,随着季节的变换,温室里的花的种类也在不断发生着 ...