##002 Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
# 链表节点都是一位数字,以上可以视为243+564=807
#先定义节点和链表类
import numpy as np
import time

class Node(object):
    def __init__(self,n,next_node=None):
        self.data=n
        self.next=next_node
    
class linklist(object):
    def __init__(self):
        self.head=None

def init(self,data):
        assert type(data)==list,type(data)
        self.head=Node(data[0],None)
        p=self.head
        for i in data[1:]:
            node=Node(i)
            p.next=node
            p=p.next

def show(self):
        l=[]
        p=self.head
        while p:
            l.append(str(p.data))
            p=p.next
        print('->'.join(l))

l1,l2=[],[]
x=1000000
t=time.time()
for i in range(x):
    l1.append(np.random.randint(0,10))
    l2.append(np.random.randint(0,10))
t=time.time()-t
print('%s 元素用时 %s s'%(x,t))
t=time.time()
ll1,ll2=linklist(),linklist()
ll1.init(l1)
ll2.init(l2)
#ll1.show()
#ll2.show()
p1,p2=ll1.head,ll2.head
ll3=linklist()
flg=0
while p1 and p2:
    num=p1.data+p2.data+flg
    p3=ll3.head
    ll3.head=Node(num%10)
    ll3.head.next=p3
    p1,p2=p1.next,p2.next
    flg=0
    if num>9:
        flg=1
if flg==1:
    p3=ll3.head
    ll3.head=Node(1)
    ll3.head.next=p3
t=time.time()-t
print('%s 元素用时 %s s'%(x,t))
#ll3.show()

leetcode python 002的更多相关文章

  1. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  2. LeetCode python实现题解(持续更新)

    目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...

  3. [LeetCode][Python]Container With Most Water

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...

  4. LeetCode Python 位操作 1

    Python 位操作: 按位与 &, 按位或 | 体会不到 按位异或 ^ num ^ num = 0 左移 << num << 1 == num * 2**1 右移 & ...

  5. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  6. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  7. [Leetcode][Python]55: Jump Game

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 55: Jump Gamehttps://leetcode.com/probl ...

  8. [Leetcode][Python]54: Spiral Matrix

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 54: Spiral Matrixhttps://leetcode.com/p ...

  9. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

随机推荐

  1. English Voice of <<City of stars>>

    City of stars 星光之城啊 Are you shining just for me? 你是否只愿为我闪耀 City of stars 星光之城啊 There's so much that ...

  2. JS实现继承的6种方式

    使用pretotype,call实现完美继承 父类: fuction Animal(name){     this.name=name||"Animal";     this.sl ...

  3. C#方式操作Cookie

    1.设置cookie public static void SetCookie(string TokenValue) { HttpCookie tokencookie = new HttpCookie ...

  4. Hadoop/HBase Capacity Planning

    http://blog.cloudera.com/blog/2010/08/hadoophbase-capacity-planning/

  5. Count Up Down(上下计数)

    这个题目是 Kayak 发布的代码挑战题目. 最简单的描述就是不使用循环,输出 0 到 5,然后同样不是会用循环的方式再次输出 5 到 0. 英文描述 Part 1 Write a program t ...

  6. js将字符串转json

    Json格式字符串 "{"rows":[{"date":"2018-11-19","money":" ...

  7. 4.2 面向对象分析(二) CRC方法标识概念类

    CRC  又称为CRC索引卡片:CRC card  每张卡片代表一个类 Each card represents one class  每张卡片上写出这个类承担的职责.与其合作交互的其他类名   ...

  8. 【洛谷p1403 】【AHOI2005】约数研究

    (有种失踪人口回归的感觉) 约束研究[传送门] (不过好像没有人注意到我这个蒟蒻) 好的不管它啦 最近学数论比较多,所以可能会有好多好多的数论题???(不存在的) 行吧上算法标签: 数论   数论  ...

  9. 第三周学习进度条+PSP0过程文档

    第三周学习进度条    第三周 所花时间(包括上课) 14:30-15:35(65)+19:00-21:20(140)+17:52-19:00(68)+19:10-20:45(95)+21:00-22 ...

  10. mysql创建存储过程,批量建表分表00到99

    这里以sqlyong为软件示例: --创建存储过程DELIMITER $$ CREATE PROCEDURE `createTablesWithIndex`() BEGIN DECLARE `@i` ...