def sortLinkListMerage(linkListOne, linkListTwo): # 合并两个有序的链表 if not linkListOne or not linkListOne.head.next: return linkListTwo if not linkListTwo or not linkListTwo.head.next: return linkListOne linkListRes = SingleLinkList() tempNode = linkListRes.head tempNodeOne = linkListOne.head.next tempNodeTwo = linkListTwo.head.next while tempNodeOne and tempNodeTwo: if tempNodeOne.data > tempNodeTwo.data: tempNode.next = tempNodeTwo tempNodeTwo = tempNodeTwo.next else: tempNode.next = tempNodeOne tempNodeOne = tempNodeOne.next tempNode = tempNode.next if not tempNodeOne: # tempNodeOne没有了,剩下的tempNodeTwo接上 tempNode.next = tempNodeTwo pass if not tempNodeTwo: # tempNodeTwo没有了,剩下的tempNodeOne接上 tempNode.next = tempNodeOne linkListRes.size = linkListOne.size + linkListTwo.size return linkListRes
class Stack(): # 栈的链式实现 def __init__(self): self.linkList = SingleLinkList() self.size = self.linkList.size def push(self, value): self.linkList.insertFisrt(value=value) self.size += 1 def pop(self): tempNode = self.linkList.delFirst() self.size -= 1 return tempNode def __str__(self): data = { 'data': [], 'size': self.size } tempNode = self.linkList.head.next if self.size == 0: return json.dumps(data) while (tempNode): data['data'].append(tempNode.data) tempNode = tempNode.next return json.dumps(data)
index = int(size / 2) else: index = size // 2 + 1 return singleLinkList.find(index) def delReverseNNode(singleLinkList, n): # 删除链表中倒数第n个节点 if n <= 0 or n > singleLinkList.size: raise ValueError('n is out of range!') preNNodeIndex = singleLinkList.size - n # 找到倒数第n个节点的前一个节点 preNNode = singleLinkList.find(preNNodeIndex) preNNode.next = preNNode.next.next if __name__ == '__main__': singleLinkList1 = SingleLinkList() singleLinkList2 = SingleLinkList() singleLinkList1.insertLast(1) singleLinkList1.insertLast(3) singleLinkList1.insertLast(5) singleLinkList2.insertLast(2) singleLinkList2.insertLast(4) singleLinkList2.insertLast(6) singleLinkList2.insertLast(8) singleLinkList2.insertLast(10) linkList = sortLinkListMerage(linkListOne=singleLinkList1, linkListTwo=singleLinkList2) print(linkList) # delReverseNNode(singleLinkList, 2) # print(singleLinkList)
def __init__(self): self.linkList = SingleLinkList() self.size = self.linkList.size self.head = self.linkList.head self.tail = self.linkList.head
def __init__(self): self.linkList = SingleLinkList() self.size = self.linkList.size
def __init__(self, size=100): self.size = size self.dataList = [] for i in range(self.size): self.dataList.append(SingleLinkList())