Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Merge Two Sorted Lists problem solution

YASH PAL, 31 July 2024

In this Leetcode Merge Two Sorted Lists problem solution we need to Merge two sorted linked lists and return them as a sorted list. The list should be made by splicing together the nodes of the first two lists.

Leetcode Merge Two Sorted Lists problem solution

Problem solution in Python.

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        prehead = ListNode(-1)

        curr = prehead
        while l1 and l2:
            if l1.val <= l2.val:
                curr.next = l1
                l1 = l1.next
            else:
                curr.next = l2
                l2 = l2.next            
            curr = curr.next

        if l1 is not None:
            curr.next = l1
        else:
            curr.next = l2

        return prehead.next

Problem solution in Java.

class Solution {
    
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode tmp = new ListNode(0);
        ListNode start = tmp;
        
        while (l1 != null || l2 != null) {
            if (l1 == null) {
                tmp.next = new ListNode(l2.val);
                tmp = tmp.next;
                l2 = l2.next;
            } else if (l2 == null) {
                tmp.next = new ListNode(l1.val);
                tmp = tmp.next;
                l1 = l1.next;
            } else {
                if (l1.val < l2.val) {
                    tmp.next = new ListNode(l1.val);                    
                    tmp = tmp.next;
                    l1 = l1.next;
                } else {
                    tmp.next = new ListNode(l2.val);
                    tmp = tmp.next;
                    l2 = l2.next;
                }
            }
        }
        
        return start.next;
    }
}

Problem solution in C++.

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
    struct ListNode* head = NULL;
    struct ListNode* p= NULL;
    
    if(!l1){
        return l2;
    }
    if(!l2){
        return l1;
    }
    
    if(l2->val > l1->val){
        head = p = l1;
        l1 = l1->next;
    }
    else{
        head = p = l2;
        l2 = l2->next;
    }
    
    while(l1 && l2){
       if(l1->val > l2->val){
           p->next = l2;
           p = l2;
           l2 = l2->next;
       } 
       else{
           p->next = l1;
           p = l1;
           l1 = l1->next;
       }
    }
    
    if(l1){
        p->next = l1;
    }
    
    if(l2){
        p->next = l2;
    }
    
    
    return head;
}

Problem solution in C.

struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
    if(l2==NULL)
        return l1;
    else if(l1==NULL)
        return l2;
    if(l1->val<=l2->val){
        l1->next=mergeTwoLists(l1->next,l2);
        return l1;
     }
    else {
        l2->next=mergeTwoLists(l1,l2->next);
        return l2;
    }
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes