DEV Community

Debesh P.
Debesh P.

Posted on

21. Merge Two Sorted Lists | LeetCode | Top Interview 150 | Coding Questions

Problem Link

https://leetcode.com/problems/merge-two-sorted-lists/


Detailed Step-by-Step Explanation

https://leetcode.com/problems/merge-two-sorted-lists/solutions/7587317/beats-1000-step-by-step-explanation-all-bkmtk


leetcode 21


Solution

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

        ListNode result = new ListNode(0);
        ListNode ptr = result;

        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                ptr.next = list1;
                list1 = list1.next;
            } else {
                ptr.next = list2;
                list2 = list2.next;
            }
            ptr = ptr.next;
        }

        if (list1 == null) ptr.next = list2;
        if (list2 == null) ptr.next = list1;

        return result.next;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)