# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeTwoLists(self, list1, list2): n1=[] n2=[] l1=list1 l2=list2 while True: if (not l1): break n1.append(l1.val) l1=l1.next while True: if (not l2): break n2.append(l2.val) l2=l2.next n=n1+n2 n=list(sorted(n,reverse=True)) result=ListNode() for i in n: result.val=i result=ListNode(next=result) result=result.next return result
解题思路:
先分别把两个链表里的数据取出来放进两个列表里面
然后把两个列表进行合并
从大到小进行排序
然后再转化为链表的格式
最后返回即可
(成绩挺不错)
关注我,在Leetcode专栏查看更多题目的解题思路吧!