Leetcode#23. Merge k Sorted Lists
ProblemYou are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
Example 1:
1234567891011Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,4,4,5,6]Explanation: The linked-lists are:[ 1->4->5, 1->3->4, 2->6]merging them into one sorted list:1->1->2->3->4->4->5->6
Example 2:
123Input: lists = []Output: []
Example 3:
123Input: lists = [[]]Output: [ ...
Leetcode#148. Sort Listk
ProblemGiven the head of a linked list, return the list after sorting it in ascending order.
Example 1:
123Input: head = [4,2,1,3]Output: [1,2,3,4]
Example 2:
123Input: head = [-1,5,3,4,0]Output: [-1,0,3,4,5]
Example 3:
12Input: head = []Output: []
Solve1234567891011121314151617181920212223242526272829303132333435363738394041424344class Solution: def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: # 沒有 或只只有一個 代表已經排好了 if not head or not head.next: ...