题解 | #合并k个已排序的链表# O(n) O(n)
合并k个已排序的链表
https://www.nowcoder.com/practice/65cfde9e5b9b4cf2b6bafa5f3ef33fa6
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * * @param lists ListNode类一维数组 * @return ListNode类 */ func mergeKLists(lists []*ListNode) *ListNode { // write code here nodeMap := make(map[int][]*ListNode) for _, list := range lists { for list != nil { if _, ok := nodeMap[list.Val]; !ok { nodeMap[list.Val] = make([]*ListNode, 0) } nodeMap[list.Val] = append(nodeMap[list.Val], list) list=list.Next } } head := &ListNode{} res := head var i int for i = -1000; i < 1000; i++ { for _, node := range nodeMap[i] { head.Next = node head = head.Next } } return res.Next }