21. Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

Solution

(1) Java

Time complexity is O(m+n), space complexity is O(1)

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode head = null;
        ListNode p = null;
        if (l1.val <= l2.val) {
            head = l1;
            p = l1;
            l1 = l1.next;
        } else {
            head = l2;
            p = l2;
            l2 = l2.next;
        }
        while (l1 != null && l2 != null) {
            if (l1.val <= l2.val) {
                p.next = l1;
                p = l1;
                l1 = l1.next;
            } else {
                p.next = l2;
                p = l2;
                l2 = l2.next;
            }
        }
        if (l1 == null) {
            p.next = l2;
        } else if (l2 == null) {
            p.next = l1;
        }
        return head;
    }
}

(2) Python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        if not l1 or l2 and l1.val > l2.val:
            l1, l2 = l2, l1
        if l1:
            l1.next = self.mergeTwoLists(l1.next, l2)
        return l1

(3) Scala

/**
 * Definition for singly-linked list.
 * class ListNode(var _x: Int = 0) {
 *   var next: ListNode = null
 *   var x: Int = _x
 * }
 */
object Solution {
    def mergeTwoLists(l1: ListNode, l2: ListNode): ListNode = {
        if (l1 == null) {
            return l2
        }
        if (l2 == null) {
            return l1
        }
        if (l1.x > l2.x) {
            l2.next = mergeTwoLists(l2.next, l1)
            return l2
        } else {
            l1.next = mergeTwoLists(l1.next, l2)
            return l1
        }
    }
}

Iterative is more faster than the above recurse

object Solution {
    def mergeTwoLists(l1: ListNode, l2: ListNode): ListNode = {
        if (l1 == null) {
            return l2
        }
        if (l2 == null) {
            return l1
        }
        val dummy = ListNode(0)
        var prev = dummy
        var n1 = l1
        var n2 = l2
        while (n1 != null && n2 != null) {
            if (n1.x <= n2.x) {
                prev.next = n1
                prev = n1
                n1 = n1.next
            } else {
                prev.next = n2
                prev = n2
                n2 = n2.next
            }
        }
        if (n1 != null) {
            prev.next = n1
        } else {
            prev.next = n2
        }
        return dummy.next
    }
}

results matching ""

    No results matching ""