lc21~lc30
21.
class ListNode: def __init__(self,x): self.val = x self.next = None class Solution: def mergeTwoLists(self, l1, l2): res = pre = ListNode(0) while l1 and l2: if l1.val <= l2.val: val = l1.val l1 = l1.next else: val = l2.val l2 = l2.next #print(val) pre.next = pre = ListNode(val) if l1: pre.next = l1 if l2: pre.next = l2 return res.next if __name__ == '__main__': L1 = ListNode(1) L1.next = L11 = ListNode(2) L11.next = L12 = ListNode(4) L2 = ListNode(1) L2.next = L21 = ListNode(3) L21.next = L22 = ListNode(4) L21.next = L22 = ListNode(5) sol = Solution() res = sol.mergeTwoLists(L1, L2) while res: print(res.val) res = res.next