Last active
June 30, 2019 16:57
-
-
Save Desolve/7b3daa901e07ed8cf45983a68f552c9e to your computer and use it in GitHub Desktop.
0021 Merge Two Sorted Lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.next = None | |
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: | |
dum = ListNode(None) | |
prev = dum | |
while l1 and l2: | |
if l1.val <= l2.val: | |
prev.next = l1 | |
l1 = l1.next | |
else: | |
prev.next = l2 | |
l2 = l2.next | |
prev = prev.next | |
if l1 == None: | |
prev.next = l2 | |
elif l2 == None: | |
prev.next = l1 | |
return dum.next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment