I am given k
numbers of LinkedList<Integer>
and I'm trying to put them into a PriorityQueue p
in order to sort it.
However, I'm having trouble trying to convert it back into a LinkedList<Integer>
from the Priority Queue that I have declared.
Here is my code:
//Code by Dave S.
public class MultiMergeWay {
public static LinkedList<Integer> mergeAll(LinkedList<Integer>[] lists){
PriorityQueue<LinkedList<Integer>> p = new PriorityQueue<>();
for(LinkedList<Integer> x : lists){
p.add(x);
}
LinkedList<Integer> array_list = new LinkedList<Integer>(p); //
return array_list;
}
}
Unless you haven't already seen it, my code wouldn't even compile because of an error in the line marked with a //
.
Can anyone please explain how can I change my priority queue back to a LinkedList?
CodePudding user response:
I think you should change a few things, if I understand the problem. First make the PriorityQueue a collection of Integer not List
PriorityQueue<Integer> p = new PriorityQueue<>();
Second in your loop.
p.addAll(x);
Then your erroneous line, should be correct
CodePudding user response:
Try below answer. To sort your LinkedList you don't need to use PriorityQueue. It will simply possible with sorted() method of stream API can help you.