[LeetCode][python3]0021. Merge Two Sorted Lists
Start the Journey
N2I -2020.07.03
1. A Common Solution
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        dummy = copy = ListNode(None)
        while l1 and l2:
            if l1.val < l2.val:
                dummy.next = l1
                l1 = l1.next
            else: 
                dummy.next = l2
                l2 = l2.next
            dummy = dummy.next
        dummy.next = l1 or l2        
        return copy.next


Explanation:
An easy way to solve this problem. Using two pointer to track the sorted list, and merge them into one.
Comments
Post a Comment