Data Structures & Algorithms

Leetcode 23: Merge K Sorted Lists (Java)

// SOLVING THIS WITH AN AI ASSISTANT (2026)

If you are working through this problem with an AI coding assistant — Claude, ChatGPT, Cursor chat, Gemini, GitHub Copilot, Aider, or any agent — the goal isn’t to ask for the answer. It is to use the tool to understand the pattern. The prompt sequence I’d run:

  1. Spec it back to me first. “In your own words, what is this problem actually testing? What’s the smallest example that fails the naive approach?”
  2. Brute-force first, optimize after. “Write the simplest correct solution, even if it’s O(n²). Don’t optimize. Just make it correct, with comments explaining each step.”
  3. Ask for the upgrade. “Now show me the optimal solution. What insight makes it possible? What pattern is this an instance of?”
  4. Stress-test it. “Generate 10 edge cases — empty input, single element, duplicates, max size, sorted, reverse-sorted. Run my solution against each.”

The pattern matters more than the answer. If the agent just hands you optimized code, you’ve trained yourself to lose interviews.

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Example:

Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6

Solution:

For this problem, the brute force solution would be passing all the lists and perform Merge Sort, but for the optimal solution we will use minHeap using PriorityQueue data structure.

Note: Please try to solve it yourself before looking into the solution below

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */class Solution {
    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>();
        
        for(ListNode head: lists){
            while(head!=null){
                minHeap.offer(head.val);
                head = head.next;
            }
        }
        
        ListNode returnNode = new ListNode(-1);
        ListNode head = returnNode;
        
        while(!minHeap.isEmpty()){
            head.next = new ListNode(minHeap.remove());
            head = head.next;
        }
        
        return returnNode.next;
    }
}

For the AI-native engineering side of HackerHeap — building MCP servers, comparing agents (Claude Code, Cursor, Windsurf, Codex, Gemini, Copilot), and weekly working code — see the Friday Build newsletter and the MCP archive.

rajendra

Recent Posts

HackerHeap is back: building with AI agents in 2026

HackerHeap is back: a multi-platform resource for working developers building with AI coding agents. We…

1 week ago

LeetCode Maximum Erasure Value

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

4 years ago

Largest Unique Number Java Solution

// BUILDING THIS WITH AN AI AGENT (2026)Whether you are using Claude Code, Cursor, Windsurf,…

5 years ago

Jump Search Algorithm In Java

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

Knuth Morris Pratt Pattern Search Algorithm

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

Binary Search Algorithm In Java

// SOLVING THIS WITH AN AI ASSISTANT (2026)If you are working through this problem with…

6 years ago

This website uses cookies.