Skip to content

Commit 15a1536

Browse files
authored
feat: add solutions to lc problem: No.0023 (#1429)
No.0023.Merge k Sorted Lists
1 parent 57c5f77 commit 15a1536

File tree

11 files changed

+476
-799
lines changed

11 files changed

+476
-799
lines changed

solution/0000-0099/0023.Merge k Sorted Lists/README.md

Lines changed: 138 additions & 250 deletions
Large diffs are not rendered by default.

solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md

Lines changed: 135 additions & 243 deletions
Large diffs are not rendered by default.
Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* struct ListNode {
4-
* int val;
5-
* ListNode *next;
6-
* ListNode() : val(0), next(nullptr) {}
7-
* ListNode(int x) : val(x), next(nullptr) {}
8-
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9-
* };
10-
*/
11-
class Solution {
12-
public:
13-
ListNode* mergeKLists(vector<ListNode*>& lists) {
14-
int n = lists.size();
15-
if (n == 0) return nullptr;
16-
for (int i = 1; i < n; ++i) lists[i] = mergeTwoLists(lists[i - 1], lists[i]);
17-
return lists[n - 1];
18-
}
19-
20-
private:
21-
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
22-
ListNode* dummy = new ListNode();
23-
ListNode* cur = dummy;
24-
while (l1 && l2) {
25-
if (l1->val <= l2->val) {
26-
cur->next = l1;
27-
l1 = l1->next;
28-
} else {
29-
cur->next = l2;
30-
l2 = l2->next;
31-
}
32-
cur = cur->next;
33-
}
34-
cur->next = l1 ? l1 : l2;
35-
return dummy->next;
36-
}
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* mergeKLists(vector<ListNode*>& lists) {
14+
auto cmp = [](ListNode* a, ListNode* b) { return a->val > b->val; };
15+
priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> pq;
16+
for (auto head : lists) {
17+
if (head) {
18+
pq.push(head);
19+
}
20+
}
21+
ListNode* dummy = new ListNode();
22+
ListNode* cur = dummy;
23+
while (!pq.empty()) {
24+
ListNode* node = pq.top();
25+
pq.pop();
26+
if (node->next) {
27+
pq.push(node->next);
28+
}
29+
cur->next = node;
30+
cur = cur->next;
31+
}
32+
return dummy->next;
33+
}
3734
};
Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* public class ListNode {
4-
* public int val;
5-
* public ListNode next;
6-
* public ListNode(int val=0, ListNode next=null) {
7-
* this.val = val;
8-
* this.next = next;
9-
* }
10-
* }
11-
*/
12-
public class Solution {
13-
public ListNode MergeKLists(ListNode[] lists) {
14-
int n = lists.Length;
15-
if (n == 0) {
16-
return null;
17-
}
18-
for (int i = 1; i < n; ++i) {
19-
lists[i] = MergeTwoLists(lists[i - 1], lists[i]);
20-
}
21-
return lists[n - 1];
22-
}
23-
24-
private ListNode MergeTwoLists(ListNode l1, ListNode l2) {
25-
ListNode dummy = new ListNode();
26-
ListNode cur = dummy;
27-
while (l1 != null && l2 != null) {
28-
if (l1.val <= l2.val) {
29-
cur.next = l1;
30-
l1 = l1.next;
31-
} else {
32-
cur.next = l2;
33-
l2 = l2.next;
34-
}
35-
cur = cur.next;
36-
}
37-
cur.next = l1 == null ? l2 : l1;
38-
return dummy.next;
39-
}
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode MergeKLists(ListNode[] lists) {
14+
PriorityQueue<ListNode, int> pq = new PriorityQueue<ListNode, int>();
15+
foreach (var head in lists) {
16+
if (head != null) {
17+
pq.Enqueue(head, head.val);
18+
}
19+
}
20+
var dummy = new ListNode();
21+
var cur = dummy;
22+
while (pq.Count > 0) {
23+
var node = pq.Dequeue();
24+
cur.next = node;
25+
cur = cur.next;
26+
if (node.next != null) {
27+
pq.Enqueue(node.next, node.next.val);
28+
}
29+
}
30+
return dummy.next;
31+
}
4032
}

solution/0000-0099/0023.Merge k Sorted Lists/Solution.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,30 @@
55
* Next *ListNode
66
* }
77
*/
8-
func mergeKLists(lists []*ListNode) *ListNode {
9-
n := len(lists)
10-
if n == 0 {
11-
return nil
12-
}
13-
for i := 1; i < n; i++ {
14-
lists[i] = mergeTwoLists(lists[i-1], lists[i])
8+
func mergeKLists(lists []*ListNode) *ListNode {
9+
pq := hp{}
10+
for _, head := range lists {
11+
if head != nil {
12+
pq = append(pq, head)
13+
}
1514
}
16-
return lists[n-1]
17-
}
18-
19-
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
15+
heap.Init(&pq)
2016
dummy := &ListNode{}
2117
cur := dummy
22-
for l1 != nil && l2 != nil {
23-
if l1.Val <= l2.Val {
24-
cur.Next = l1
25-
l1 = l1.Next
26-
} else {
27-
cur.Next = l2
28-
l2 = l2.Next
29-
}
18+
for len(pq) > 0 {
19+
cur.Next = heap.Pop(&pq).(*ListNode)
3020
cur = cur.Next
31-
}
32-
if l1 != nil {
33-
cur.Next = l1
34-
} else if l2 != nil {
35-
cur.Next = l2
21+
if cur.Next != nil {
22+
heap.Push(&pq, cur.Next)
23+
}
3624
}
3725
return dummy.Next
38-
}
26+
}
27+
28+
type hp []*ListNode
29+
30+
func (h hp) Len() int { return len(h) }
31+
func (h hp) Less(i, j int) bool { return h[i].Val < h[j].Val }
32+
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
33+
func (h *hp) Push(v any) { *h = append(*h, v.(*ListNode)) }
34+
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
1-
/**
2-
* Definition for singly-linked list.
3-
* public class ListNode {
4-
* int val;
5-
* ListNode next;
6-
* ListNode() {}
7-
* ListNode(int val) { this.val = val; }
8-
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9-
* }
10-
*/
11-
class Solution {
12-
public ListNode mergeKLists(ListNode[] lists) {
13-
int n = lists.length;
14-
if (n == 0) {
15-
return null;
16-
}
17-
for (int i = 0; i < n - 1; ++i) {
18-
lists[i + 1] = mergeLists(lists[i], lists[i + 1]);
19-
}
20-
return lists[n - 1];
21-
}
22-
23-
private ListNode mergeLists(ListNode l1, ListNode l2) {
24-
ListNode dummy = new ListNode();
25-
ListNode cur = dummy;
26-
while (l1 != null && l2 != null) {
27-
if (l1.val <= l2.val) {
28-
cur.next = l1;
29-
l1 = l1.next;
30-
} else {
31-
cur.next = l2;
32-
l2 = l2.next;
33-
}
34-
cur = cur.next;
35-
}
36-
cur.next = l1 == null ? l2 : l1;
37-
return dummy.next;
38-
}
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeKLists(ListNode[] lists) {
13+
PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> a.val - b.val);
14+
for (ListNode head : lists) {
15+
if (head != null) {
16+
pq.offer(head);
17+
}
18+
}
19+
ListNode dummy = new ListNode();
20+
ListNode cur = dummy;
21+
while (!pq.isEmpty()) {
22+
ListNode node = pq.poll();
23+
if (node.next != null) {
24+
pq.offer(node.next);
25+
}
26+
cur.next = node;
27+
cur = cur.next;
28+
}
29+
return dummy.next;
30+
}
3931
}

solution/0000-0099/0023.Merge k Sorted Lists/Solution.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,21 @@
1010
* @return {ListNode}
1111
*/
1212
var mergeKLists = function (lists) {
13-
const n = lists.length;
14-
if (n == 0) {
15-
return null;
16-
}
17-
for (let i = 1; i < n; ++i) {
18-
lists[i] = mergeTwoLists(lists[i - 1], lists[i]);
13+
const pq = new MinPriorityQueue({ priority: node => node.val });
14+
for (const head of lists) {
15+
if (head) {
16+
pq.enqueue(head);
17+
}
1918
}
20-
return lists[n - 1];
21-
};
22-
23-
function mergeTwoLists(l1, l2) {
2419
const dummy = new ListNode();
2520
let cur = dummy;
26-
while (l1 && l2) {
27-
if (l1.val <= l2.val) {
28-
cur.next = l1;
29-
l1 = l1.next;
30-
} else {
31-
cur.next = l2;
32-
l2 = l2.next;
33-
}
21+
while (!pq.isEmpty()) {
22+
const node = pq.dequeue().element;
23+
cur.next = node;
3424
cur = cur.next;
25+
if (node.next) {
26+
pq.enqueue(node.next);
27+
}
3528
}
36-
cur.next = l1 || l2;
3729
return dummy.next;
38-
}
30+
};
Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
6-
class Solution:
7-
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
8-
n = len(lists)
9-
if n == 0:
10-
return None
11-
for i in range(n - 1):
12-
lists[i + 1] = self.mergeTwoLists(lists[i], lists[i + 1])
13-
return lists[-1]
14-
15-
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
16-
dummy = ListNode()
17-
cur = dummy
18-
while l1 and l2:
19-
if l1.val <= l2.val:
20-
cur.next = l1
21-
l1 = l1.next
22-
else:
23-
cur.next = l2
24-
l2 = l2.next
25-
cur = cur.next
26-
cur.next = l1 or l2
27-
return dummy.next
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
8+
setattr(ListNode, "__lt__", lambda a, b: a.val < b.val)
9+
pq = [head for head in lists if head]
10+
heapify(pq)
11+
dummy = cur = ListNode()
12+
while pq:
13+
node = heappop(pq)
14+
if node.next:
15+
heappush(pq, node.next)
16+
cur.next = node
17+
cur = cur.next
18+
return dummy.next

solution/0000-0099/0023.Merge k Sorted Lists/Solution.rb

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)