w3resource

Java: Merge two given sorted lists


Merge Two Sorted Linked Lists

Write a Java program to merge the two sorted linked lists.

Sample Solution:

Java Code:

import java.util.*
public class Solution {
    public static void main(String[] args) {
        // Create two sorted linked lists
        ListNode list1 = new ListNode(1);
        list1.next = new ListNode(3);
        list1.next.next = new ListNode(7);
        list1.next.next.next = new ListNode(9);
        list1.next.next.next.next = new ListNode(13);
        ListNode list2 = new ListNode(2);
        list2.next = new ListNode(40);
        
        // Merge the two sorted lists and get the result
        ListNode head = mergeTwoLists(list1, list2);
		System.out.print("Merge Two Sorted Lists:\n");
        
        // Print the merged list
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
    }

    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        // Create a new linked list for the merged result
        ListNode head = new ListNode(0);
        ListNode mlist = head;
        
        // Merge the two lists while maintaining the order
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                mlist.next = new ListNode(list1.val);
                mlist = mlist.next;
                list1 = list1.next;
            } else {
                mlist.next = new ListNode(list2.val);
                mlist = mlist.next;
                list2 = list2.next;
            }
        }
        
        // Append any remaining elements from list1
        while (list1 != null) {
            mlist.next = new ListNode(list1.val);
            mlist = mlist.next;
            list1 = list1.next;
        }
        
        // Append any remaining elements from list2
        while (list2 != null) {
            mlist.next = new ListNode(list2.val);
            mlist = mlist.next;
            list2 = list2.next;
        }
        
        // Skip the dummy head node and return the merged list
        head = head.next;
        return head;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode(int val) {
        this.val = val;
        this.next = null;
    }
}

Sample Output:

Merge Two Sorted ListsT:
1 2 3 7 9 13 40 

Flowchart:

Flowchart: Java exercises: Merge two given sorted lists.


For more Practice: Solve these Related Problems:

  • Write a Java program to merge two sorted linked lists without using extra space.
  • Write a Java program to merge two sorted linked lists in alternating order.
  • Write a Java program to merge multiple sorted linked lists into a single sorted list.
  • Write a Java program to merge two sorted linked lists while keeping only distinct elements.

Go to:


PREV : Check Anagrams.
NEXT : Remove Element in Array.


Java Code Editor:

Company:  Microsoft Apple LinkedIn Amazon

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.