Italiano
Apri in Telegram

Postimport java.util.PriorityQueue;

9 febbraio 2025
D
Deloitte | COGNIZANT | IBM | CAPGEMINI
import java.util.PriorityQueue; import java.util.Scanner; class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } public class Main { public static ListNode mergeKLists(ListNode[] lists) { PriorityQueue<ListNode> minHeap = new PriorityQueue<>((a, b) -> a.val - b.val); for (ListNode list : lists) { if (list != null) { minHeap.add(list); } } ListNode dummy = new ListNode(-1); ListNode current = dummy; while (!minHeap.isEmpty()) { ListNode smallest = minHeap.poll(); current.next = smallest; current = current.next; if (smallest.next != null) { minHeap.add(smallest.next); } } return dummy.next; } public static ListNode arrayToLinkedList(int[] arr) { if (arr.length == 0) return null; ListNode head = new ListNode(arr[0]); ListNode current = head; for (int i = 1; i < arr.length; i++) { current.next = new ListNode(arr[i]); current = current.next; } return head; } public static void printLinkedList(ListNode head) { ListNode current = head; while (current != null) { System.out.print(current.val + " "); current = current.next; } System.out.println(); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int k = scanner.nextInt(); // Number of linked lists ListNode[] lists = new ListNode[k]; scanner.nextLine(); // Move to next line for (int i = 0; i < k; i++) { String[] elements = scanner.nextLine().split(" "); int[] arr = new int[elements.length]; for (int j = 0; j < elements.length; j++) { arr[j] = Integer.parseInt(elements[j]); } lists[i] = arrayToLinkedList(arr); } ListNode mergedHead = mergeKLists(lists); printLinkedList(mergedHead); scanner.close(); } } #MERGE_K_SORTED_ARRAYS #L&T
744 ·

Vicino nel feed

DDeloitte | COGNIZANT | IBM | CAPGEMINIWIPRO EXAM ✅✅✅ Contact: @MLCODER2DDeloitte | COGNIZANT | IBM | CAPGEMINIWipro exam ✅✅✅ Contact: @MLCODER2
questo messaggio
DDeloitte | COGNIZANT | IBM | CAPGEMINIimport java.util.*; class Vehicle { protected String make; protected String model; protected int year; public Vehicle(String make, String model, int year) { thiDDeloitte | COGNIZANT | IBM | CAPGEMINIcase "update car": System.out.print("Enter car model to update: "); String carToUpdate = scanner.nextLine(); if (carInventory.containsKey(ca
DDeloitte | COGNIZANT | IBM | CAPGEMINIDeloitte | COGNIZANT | IBM | CAPGEMINI@python_java_Coding · canale · Tecnologia
7 178iscritti406copertura media dei post
Feed della community Apri in Telegram

Un feed pubblico aperto dall'indice di ricerca ChatCrawler — «Google per il Telegram pubblico»; si aggiorna man mano che la community viene scansionata. Orari in UTC.

Solo contenuti pubblici, API ufficiale di Telegram. Informazioni · FAQ · Limiti dell'indice · Rimuovi una pagina · Catalogo · Ricerca · Come calcoliamo