Merge Two Sorted Lists

I did this Leetcode question. Here is my code:

class Solution(object):
    def mergeTwoLists(self, list1, list2):
        """
        :type list1: Optional[ListNode]
        :type list2: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        nlist = ListNode()
        ptr = nlist

        while list1 and list2:
            if list1.val < list2.val:
                ptr.next = list1
                list1 = list1.next
            else:
                ptr.next = list2
                list2 = list2.next
            ptr = ptr.next

        ptr.next = list1 or list2

        return nlist.next
Previous
Previous

Linked Lists

Next
Next

Reverse Linked List