-
Notifications
You must be signed in to change notification settings - Fork 481
/
0021.py
59 lines (49 loc) · 1.24 KB
/
0021.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
h = ListNode(-1)
cur = h
cur1 = l1
cur2 = l2
while cur1 != None and cur2 != None:
if cur1.val <= cur2.val:
cur.next = cur1
cur1 = cur1.next
else:
cur.next = cur2
cur2 = cur2.next
cur = cur.next
if cur1 != None:
cur.next = cur1
if cur2 != None:
cur.next = cur2
return h.next
def createList():
head = ListNode(0)
cur = head
cur.next = ListNode(0)
cur.next.next = ListNode(1)
cur.next.next.next = ListNode(2)
return head
def printList(head):
cur = head
while cur != None:
print(cur.val, '-->', end='')
cur = cur.next
print('NULL')
if __name__ == "__main__":
l1 = createList()
printList(l1)
l2 = createList()
printList(l2)
res = Solution().mergeTwoLists(l1, l2)
printList(res)