Skip to content

Instantly share code, notes, and snippets.

@Desolve
Last active June 30, 2019 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Desolve/7b3daa901e07ed8cf45983a68f552c9e to your computer and use it in GitHub Desktop.
Save Desolve/7b3daa901e07ed8cf45983a68f552c9e to your computer and use it in GitHub Desktop.
0021 Merge Two Sorted Lists
# 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