看到 Leecode 的题目,发现 python 的数据结构,但是工作中目前没有用到链表这个结构,尴尬
#-*- coding: utf-8 -*-
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x, Node=None):
self.val = x
self.next = Node
def __repr__(self):
'''
用来定义Node的字符输出,
print为输出data
'''
return str(self.val)
class Chain(object):
def __init__(self,head=None,length=0):
self.head = head
self.length = length
def isEmpty(self):
return (self.length == 0)
def appendNode(self,dataOrNode):#在末尾增加一个节点
item = None
if isinstance(item,ListNode):
item = dataOrNode
else:
item = ListNode(dataOrNode)
if not self.head:
self.head = item
self.length += 1
else:
node = self.head
while node.next: #遍历到最后一个节点
node = node.next
node.next = item
self.length += 1
def deleteNode(self,index):
if self.isEmpty():
print "this chain table is empty."
return
if index <0 or index >= self.length:
print 'error: out of index'
return
if index == 0:
self.head = self.head.next
self.length -= 1
return
#遍历到该index,记录这个index节点的前一个跟后一个
#将前一个节点的next 指向下个节点就Over
#pre_node 记录index的前一个节点,node记录当前节点
start = 0##第一个节点位置
pre_node = self.head
node = self.head
while node.next and start < index:
pre_node = node
node = pre_node.next
start += 1
if start == index:
index_node = node
pre_node.next = node.next
self.length -= 1
def updateNode(self,index,data):
if index <0 or index >= self.length:
print 'error: out of index'
return
start = 0 ##第一个节点位置
node = self.head
while node.next and start < index:
node = node.next
start += 1
if start == index:
node.val = data
def getItem(self,index):
if index < 0 or index >= self.length:
print 'error: out of index'
return
start = 0 ##第一个节点位置
node = self.head
while node.next and start < index:
node = node.next
start += 1
return node.val
def getIndex(self,data):
if self.isEmpty():
print "this chain table is empty."
return
start = 0
node = self.head
while node:
if node.val == data:
return start
else:
node = node.next
start += 1
if start == self.length:
print "%s not found" % str(data)
return
def insertNode(self,index,data):
item = None
if isinstance(item, data):
item = data
else:
item = ListNode(data)
if index <0 or index >= self.length:
print 'error: out of index'
return
if index == 0:
head = self.head
self.head = data
self.head.next = head
self.length += 1
return
else:
start = 0 ##第一个节点位置
pre_node = self.head
next_node = self.head
while next_node.next and start < index:
pre_node = next_node
next_node = pre_node.next
start += 1
if start == index:
pre_node.next = item
item.next = next_node
self.length += 1
def clear(self):
self.head = None
self.length = 0
def generateNonde(self,items1, items2):
items1_list = []
items2_list = []
for i in items1:
if i in ['0','1','2','3','4','5','6','7','8','9']:
items1_list.append(int(i))
for y in items2:
if y in ['0','1','2','3','4','5','6','7','8','9']:
items2_list.append(int(y))
chain1 = Chain()
chain2 = Chain()
for i in items1_list:
chain1.appendNode(i)
for y in items2_list:
chain2.appendNode(y)
return chain1, chain2, chain1.length,chain2.length
class Solution(object):
def addTwoNumbers(self, l1, l2,length1,length2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
list1_str = ''
list2_str = ''
chain3 = Chain()
for i in range(length1, 0, -1):
list1_str += str(l1.getItem(i - 1))
for y in range(length2, 0, -1):
list2_str += str(l2.getItem(y - 1))
result = int(list1_str) + int(list2_str)
result = str(result)
print result, type(result)
for i in range(len(result), 0, -1):
chain3.appendNode(int(result[i - 1]))
return [chain3.getItem(i) for i in range(chain3.length)]
if __name__ == '__main__':
chain1, chain2, length1,length2 = Chain().generateNonde("[2,4,3]", "[7,7,9]")
result = Solution().addTwoNumbers(chain1, chain2,length1,length2)
print result
# for i in range(result.length):
# print result.getItem(i)