Leetcode python 002

Source: Internet
Author: User

# #002 Input: (2, 4, 3) + (5, 6, 4) output:7, 0, 8
# linked list nodes are a single number, which can be considered 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 ('-a '. 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 element spents%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 element spents%s s '% (x,t))
#ll3. Show ()

Leetcode python 002

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.