← Back to list

Dummy Node Trick — The Clean Way to Merge Two Sorted Lists

Today i went through the Leetcode Famous linked list problem was : 23. Merge k Sorted Lists. So when i started to solve this i was blank…

Ramnayan Yadav · 2026-05-25 16:38 · 0 claps · 2.9 min read
#coding #javascript #javascript-tips #linked-lists #data-structures
Open on Medium ↗
Wiki topics: 💻 · Programming 🌐 · Web Development

Dummy Node Trick — The Clean Way to Merge Two Sorted Lists

Today i went through the Leetcode Famous linked list problem was : ***23. Merge k Sorted Lists. *So when i started to solve this i was blank how to manage K lists.

I found a hint from the problem : **21. Merge Two Sorted Lists. **which i had already solved before. In this post i’ll explain this problem line by line.

let’s take an example and understand this.

Problem statement:

You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.

Example 1:

Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Constraints:

The number of nodes in both lists is in the range [0, 50].
-100 <= Node.val <= 100
Both list1 and list2 are sorted in non-decreasing order.

As you can see above example two list given we have to merge them into single list in sorted order. I’ll use the simplest approach to solve this by comparing from start of the both lists.

Psuedo code:

temp = new Node(0);
node = temp;

   loop list1!=null && list2!=null
      if list1[i].value >= list2[i].value
          temp.next = list2[i];
          list2=list.next
       lese
          temp.next = list1[i]
          list1=list1.next;
    temp=temp.next;

// if list have different size/length
if list1!=null
  temp.next=list1;
else temp.next=list2;

//return node.next beacouse deafult value 0 added when node created.
return node.next;

I hope you have understand the problem and approch how to fix it. if you wanna more reference please find the attached references from *geeksforgeeks and InterviewBit etc.*

I have used the anthropic to exaplain/visulaize this problem line-by line.

Line by Line Explanation

Line 1

“You are given the heads of two sorted linked lists list1 and list2.”

You are not given the full array — you are given only the starting node (head) of each linked list. Each list is already sorted in order.

list1:  1 → 2 → 4 → null
        ↑
       head  (this is what you receive)
list2:  1 → 3 → 4 → null
        ↑
       head

Line 2

“Merge the two lists into one sorted list.”

Combine both lists so the final result is a single sorted linked list containing all elements from both.

list1:  1 → 2 → 4
list2:  1 → 3 → 4
merged: 1 → 1 → 2 → 3 → 4 → 4

Line 3

“The list should be made by splicing together the nodes of the first two lists.”

This is the key constraint — do not create new nodes. Instead reuse and relink the existing nodes from list1 and list2 by changing their next pointers.

❌ Wrong approach — creating new nodes
   new Node(1) → new Node(1) → new Node(2) ...
✅ Right approach — relinking existing nodes
   point list1's node.next or list2's node.next to each other

Line 4

“Return the head of the merged linked list.”

After merging, return only the first node of the final merged list. The interviewer can traverse from there to see the full result.

merged: 1 → 1 → 2 → 3 → 4 → 4 → null
        ↑
     return this

Example Walkthrough

***list1 = [1,2,4], list2 = [1,3,4] → Output: [1,1,2,3,4,4]*---

Constraint — [0, 50] nodes, values [-100, 100]

The number of nodes is in range [0, 50]
→ either list can be EMPTY (length 0) — handle null check!
-100 <= Node.val <= 100
→ values can be negative — comparison still works normally

Key Points to Remember

Line What it Means “heads of two sorted lists” You only get the first node, not the full array “merge into one sorted list” Final output must be sorted end to end “splicing together nodes” Reuse existing nodes, don’t create new ones “return the head” Return only the first node of the merged result

One Line Summary

At each step compare the front nodes of both lists, attach the smaller one to your result, and advance that pointer — until one list runs out, then attach the remainder of the other.

If you found this useful and informative, you can leave your impressions for motivation and improvement 🙌👨🏿‍💻


메타데이터
post_id
9aa68a0eaeca
slug
dummy-node-trick-the-clean-way-to-merge-two-sorted-lists-9aa68a0eaeca
url
https://medium.com/@ramnayan699/dummy-node-trick-the-clean-way-to-merge-two-sorted-lists-9aa68a0eaeca
canonical_url
https://medium.com/@ramnayan699/dummy-node-trick-the-clean-way-to-merge-two-sorted-lists-9aa68a0eaeca
author_url
https://medium.com/@ramnayan699
status
ok
fetched_at
2026-06-09 14:34:10