-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path148. Sort List
More file actions
30 lines (27 loc) · 1003 Bytes
/
148. Sort List
File metadata and controls
30 lines (27 loc) · 1003 Bytes
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
/* USING MIN-HEAP O(nlogn), O(n) */
class Solution {
public ListNode sortList(ListNode head) {
// Use a min-heap (priority queue) to sort nodes based on their values
PriorityQueue<ListNode> pq = new PriorityQueue<>((a, b) -> a.val - b.val);
ListNode curr=head;
while(curr!=null){
pq.add(curr);
curr=curr.next;
}
ListNode dummy = new ListNode(0);
curr = dummy;
while(!pq.isEmpty()){
curr.next = pq.poll();
curr = curr.next;
}
curr.next = null; //Ensure the last node points to null
return dummy.next;
}
}
/*
LOGIC---
PQ does not know how to compare ListNodes so write logic yourself to compare their values.
TC - O(nlogn)
You iterate through the list once to add all nodes to the priority queue. This operation takes O(nlogn), where n is the number of nodes in the list. Each insertion into the heap takes O(logn), and you do this n times.
SC - O(n)
*/