Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
Input:[ 1->4->5, 1->3->4, 2->6 ]
Output:1->1->2->3->4->4->5->6
func mergeKLists(_ lists: [ListNode?]) -> ListNode? { var i = 0, j = 0, fl = lists.compactMap({$0}), n = fl.count, p: ListNode? if n < 1 { return nil } var arr = Array(repeating: 0, count: 256*256) while i < n { p = fl[i]; i += 1 while let p1 = p { arr[j] = p1.val p = p1.next; j += 1 if p == nil, i < n { p1.next = fl[i] } } } arr = Array(arr[..<j]).sorted() i = 0; p = fl[0] while let q = p, i < j { q.val = arr[i] p = q.next; i += 1 } return fl[0] }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
We Are the Nerds
Christine Lagorio-Chafkin / Hachette Books / 2018-10-2 / USD 18.30
Reddit hails itself as "the front page of the Internet." It's the third most-visited website in the United States--and yet, millions of Americans have no idea what it is. We Are the Nerds is an eng......一起来看看 《We Are the Nerds》 这本书的介绍吧!