/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/funcmergeKLists(lists[]*ListNode)*ListNode{varmergeTowListsfunc(a,b*ListNode)*ListNodemergeTowLists=func(a,b*ListNode)*ListNode{dummy:=&ListNode{}tail:=dummyfora!=nil&&b!=nil{ifa.Val<b.Val{tail.Next=aa=a.Next}else{tail.Next=bb=b.Next}tail=tail.Next}ifa!=nil{tail.Next=a}else{tail.Next=b}returndummy.Next}varans*ListNodefor_,list:=rangelists{ans=mergeTowLists(ans,list)}returnans}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/funcmergeKLists(lists[]*ListNode)*ListNode{n:=len(lists)ifn==0{returnnil}elseifn==1{returnlists[0]}else{mid:=n/2listA:=mergeKLists(lists[0:mid])listB:=mergeKLists(lists[mid:n])dummy:=&ListNode{}tail:=dummyforlistA!=nil&&listB!=nil{iflistA.Val<listB.Val{tail.Next=listAlistA=listA.Next}else{tail.Next=listBlistB=listB.Next}tail=tail.Next}iflistA!=nil{tail.Next=listA}else{tail.Next=listB}returndummy.Next}}
typePriorityQueuestruct{arr[]*ListNode}func(pq*PriorityQueue)Less(i,jint)bool{returnpq.arr[i].Val<pq.arr[j].Val}func(pq*PriorityQueue)Push(nodeinterface{}){pq.arr=append(pq.arr,node.(*ListNode))}func(pq*PriorityQueue)Pop()interface{}{a:=pq.arrret:=a[len(a)-1]pq.arr=a[:len(a)-1]returnret}func(pq*PriorityQueue)Swap(i,jint){pq.arr[i],pq.arr[j]=pq.arr[j],pq.arr[i]}func(pq*PriorityQueue)Len()int{returnlen(pq.arr)}/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/funcmergeKLists(lists[]*ListNode)*ListNode{pq:=&PriorityQueue{}for_,list:=rangelists{iflist!=nil{heap.Push(pq,list)}}dummy:=&ListNode{}tail:=dummyforpq.Len()>0{top:=heap.Pop(pq)tail.Next=top.(*ListNode)tail=tail.Nextiftail.Next!=nil{heap.Push(pq,tail.Next)}}returndummy.Next}