题目描述:
合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
示例:
输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-k-sorted-lists
思路要点:
1、导入链表模块(自定义)
2、使用python自带的二分排序库进行排序,可降低时间复杂度
代码:
import chaintable as c
import bisect
class Solution(c.chainTable):
def __init__(self,lists):
self.lists=lists
self.result=self.mergeKLists(self.lists)
def mergeKLists(self,lists):
sort_lst=[]
while True:
flag=0
for i in lists:
if i.isEmpty():
continue
flag=1
t=i.getItem(0)
bisect.insort(sort_lst,t)
i.delete(0)
if flag==0:
break
sort_chaintable=c.chainTable()
for i in sort_lst:
sort_chaintable.append(i)
return sort_chaintable
if __name__=='__main__':
lists=['145','134','26']
for i in range(len(lists)):
temp=lists[i]
lists[i]=c.chainTable()
for j in temp:
lists[i].append(j)
print('原链表:\n')
print(lists)
print('\n')
solution=Solution(lists)
lst=solution.result
print('排序后:\n')
print(lst)
自定义的python链表:
#用python实现数据结构链表
class Node:
'''
定义节点类
data:节点保存的数据
next_node:下一个节点对象
'''
def __init__(self,data,pnext=None):
self.data=data
self.next_node=pnext
def __repr__(self):
'''
定义Node的字符输出
print为输出data
'''
return str(self.data)
'''
链表类:
属性:
链表头:head
链表长度:length
方法:以下
'''
class chainTable:
def __init__(self):
self.head=None
self.length=0
'判断是否为空列表'
def isEmpty(self):
return self.length==0
'在链表尾增加一个节点'
def append(self,dataorNode):
item = None
if isinstance(dataorNode, Node):
item = dataorNode
else:
item = Node(dataorNode)
if not self.head:
self.head=item
self.length+=1
else:
node=self.head
'循环至最后一个节点'
while node.next_node:
node=node.next_node
node.next_node=item
self.length+=1
'删除一个节点'
def delete(self,index):
if self.isEmpty():
print("Sorry,the 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_node
self.length-=1
return
Index=0
node=self.head
pre_node=self.head
while node.next_node and Index<index:
pre_node=node
node=node.next_node
Index+=1
if Index==index:
pre_node=node.next_node
self.length-=1
'修改一个节点'
def update(self,index,data):
if self.isEmpty()or index<0 or index>=self.length:
print("Error:out of index")
return
Index=0
node=self.head
while node.next_node and Index<index:
node=node.next_node
Index+=1
if Index==index:
node.data=data
'查找并得到一个节点'
def getItem(self,index):
if self.isEmpty()or index<0 or index>=self.length:
print("Error:out of index")
return
Index=0
node=self.head
while node.next_node and Index<index:
node=node.next_node
Index+=1
return node.data
'查找一个节点的索引'
def getIndex(self,data):
if self.isEmpty():
print("Sorry,the chain table is empty")
return
Index=0
node=self.head
while node:
if node.data==data:
return Index
node=node.next_node
Index+=1
if Index==self.length:
print("\"%s is not found\",%str(data)")
return
'插入一个节点'
def Insert(self,index,dataorNode):
if self.isEmpty():
print("Sorry,the chain table is empty")
return
if index<0 or index>=self.length:
print("Error:out of index")
return
item=None
if isinstance(dataorNode,Node):
item=dataorNode
else:
item=Node(dataorNode)
if index==0:
item.next_node=self.head
self.head=item
self.length+=1
return
Index=0
node=self.head
prev=self.head
while node.next_node and Index<index:
prev=node
node=node.next_node
Index+=1
if Index==index:
item.next_node=node
prev.next_node=item
self.length+=1
'清空链表'
def clear(self):
self.head=None
self.length=0
def __repr__(self):
if self.isEmpty():
return "The chain table is empty"
node=self.head
nlist=''
while node:
if node.next_node:
nlist+=str(node.data)+'->'
node=node.next_node
else:
nlist+=str(node.data)
node=node.next_node
return nlist
def __getitem__(self, ind):
if self.isEmpty() or ind < 0 or ind >= self.length:
print("error: out of index")
return
return self.getItem(ind)
def __setitem__(self, ind, val):
if self.isEmpty() or ind < 0 or ind >= self.length:
print("error: out of index")
return
self.update(ind, val)
def __len__(self):
return self.length
运行结果:
原链表:
[1->4->5, 1->3->4, 2->6]
排序后:
1->1->2->3->4->4->5->6