Sort a k sorted doubly linked list | GFG POTD 20 Oct 2024 | JAVA | C++

preview_player
Показать описание
#gfgpotd #gfgproblemoftheday #gfgtoday
#gfgpotd #gfgproblemoftheday #gfgtoday #gfgpotdtoday #potd #potdgfg #potdgfgtoday #gfgstreek #problemoftheday
Рекомендации по теме
Комментарии
Автор

great,
one suggestion, please make the code editor to full screen while coding,
the full screen button is next to the reset/settings button the in the code editor..

aizadiqbal
Автор

stack use kar skte kya sir . means 2 stack lagana padhegha

shashankchaure
Автор

JAVA Code :
public DLLNode sortAKSortedDLL(DLLNode head, int k) {
// Code here
PriorityQueue<DLLNode> pq = new PriorityQueue<>((a, b) -> a.data - b.data);
int i =0;
while(i <= k && head != null){
pq.add(head);
head = head.next;
++i;
}

DLLNode newHead = null;
DLLNode curr = null;
while(!pq.isEmpty()){
if(newHead == null){
newHead = pq.poll();
newHead.next = null;
newHead.prev = null;
curr = newHead;
}
else{
curr.next = pq.poll();
curr.next.prev = curr;
curr = curr.next;
}

if(head != null){
pq.add(head);
head = head.next;
}
}
curr.next = null;
return newHead;
}

codeCraft
welcome to shbcf.ru