# #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