일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- data science
- Class
- 30. Substring with Concatenation of All Words
- 프로그래머스
- Protocol
- attribute
- shiba
- 컴퓨터의 구조
- Generator
- 운영체제
- Regular Expression
- Python
- 109. Convert Sorted List to Binary Search Tree
- iterator
- kaggle
- Python Code
- 43. Multiply Strings
- 밴픽
- Python Implementation
- Decorator
- 315. Count of Smaller Numbers After Self
- Convert Sorted List to Binary Search Tree
- DWG
- 파이썬
- Substring with Concatenation of All Words
- 시바견
- concurrency
- 715. Range Module
- LeetCode
- t1
Archives
- Today
- Total
Scribbling
[Java101] LeetCode: 23. Merge k Sorted Lists: Priority Queue 본문
Computer Science/Java
[Java101] LeetCode: 23. Merge k Sorted Lists: Priority Queue
focalpoint 2023. 3. 2. 04:39/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists == null || lists.length == 0) {
return null;
}
PriorityQueue<ListNode> pq = new PriorityQueue<>(
lists.length, new Comparator<ListNode>() {
@Override
public int compare(ListNode node1, ListNode node2) {
if (node1.val < node2.val) {
return -1;
} else if (node1.val > node2.val) {
return 1;
} else {
return 0;
}
}
}
);
for (ListNode node: lists) {
if (node != null) pq.add(node);
}
ListNode head = new ListNode();
ListNode cur = head;
while (!pq.isEmpty()) {
ListNode polled = pq.poll();
cur.next = polled;
cur = polled;
if (polled.next != null) {
pq.add(polled.next);
}
}
cur.next = null;
return head.next;
}
}
'Computer Science > Java' 카테고리의 다른 글
[Java101] Trie Implementation (0) | 2023.03.08 |
---|---|
[Java 101] 102. Binary Tree Level Order Traversal - Queue (0) | 2023.03.04 |
[Java 101] 20. Valid Parentheses: Stack (0) | 2023.02.23 |
[Java 101] 76. Minimum Window Substring: Set, HashMap (0) | 2023.02.22 |
[Java 101] 125. Valid Palindrome with StringBuilder (0) | 2023.02.21 |