Sort A k Sorted Doubly Linked List | Linked Lists + Min Heap | MEDIUM

Поділитися
Вставка
  • Опубліковано 5 лис 2024

КОМЕНТАРІ • 3

  • @galepraveen
    @galepraveen  17 днів тому +1

    DLLNode *sortAKSortedDLL(DLLNode *head, int k) {
    priority_queue pq;
    DLLNode* tail = head;
    DLLNode* nexty = NULL;
    int freq = k + 1;
    while(tail && freq > 0){
    pq.push(tail->data);
    tail = tail->next;
    nexty = tail;
    freq -= 1;
    }
    tail = head;
    while(nexty){
    int minVal = pq.top(); pq.pop();
    tail->data = minVal;
    tail = tail->next;
    pq.push(nexty->data);
    nexty = nexty->next;
    }
    while(tail && !pq.empty()){
    int minVal = pq.top(); pq.pop();
    tail->data = minVal;
    tail = tail->next;
    }
    return head;
    }

    • @galepraveen
      @galepraveen  17 днів тому

      Issue from my side, i should also have added 5 in heap and processed the execution. Sorry for that ❤️