感受python语言的优美的写法:
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def combine(self,list2, list3):
if list2==None:
return list3
if list3==None:
return list2
dummy = ListNode(-1)
temp = dummy
while list2!=None and list3!=None:
if list2.val<=list3.val:
temp.next = list2
list2 = list2.next
temp = temp.next
else:
temp.next = list3
list3 = list3.next
temp = temp.next
if list2!=None:
temp.next = list2
if list3!=None:
temp.next = list3
return dummy.next
if __name__ == "__main__":
node1 = ListNode(2)
node2 = ListNode(3)
node3 = ListNode(4)
node1.next = node2
node2.next = node3
node3.next = None
h2 = ListNode(3)
h3 = ListNode(4)
h4 = ListNode(8)
h2.next = h3
h3.next = h4
h4.next = None
s = Solution()
res = s.combine(node1, h2)
while res:
print(res.val, end=" ")
res = res.next