Runtime: 68 ms, faster than 28.06% of Python3 online submissions for Merge Two Sorted Lists.
由于对python指针不熟悉,参考了一下其他同学的写法
这题就是标准的数据结构题目,学过数据结构的同学应该都见过类似的题目
好像是 尾插法
# 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
"""
head = ListNode(0)
l3 = head
while(l1 and l2):
if l1.val < l2.val:
l3.next = l1
l1 = l1.next
else:
l3.next = l2
l2 = l2.next
l3 = l3.next
l4 = l1 or l2
l3.next = l4
return head.next