Español
Abrir en Telegram

Publicaciónimport java.util.PriorityQueue;

9 de febrero de 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 ·

Cerca en el feed

DDeloitte | COGNIZANT | IBM | CAPGEMINIWIPRO EXAM ✅✅✅ Contact: @MLCODER2DDeloitte | COGNIZANT | IBM | CAPGEMINIWipro exam ✅✅✅ Contact: @MLCODER2
este mensaje
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 · canal · Tecnología
7 178suscriptores451alcance medio por publicación
Feed de la comunidad Abrir en Telegram

Un feed público abierto del índice de búsqueda ChatCrawler — el “Google del Telegram público”; se actualiza a medida que se rastrea la comunidad. Las horas se muestran en UTC.

Solo contenido público, API oficial de Telegram. Acerca de · Preguntas frecuentes · Límites del índice · Eliminar una página · Catálogo · Búsqueda · Cómo contamos