Problem Description:
The two ordered lists into a new sorted list and return. The new list is by all nodes in a given mosaic composed of two lists.
Example:
Input: 1-> 2-> 4, 1-> 3-> 4
Output: 1-> 1-> 2-> 3-> 4-> 4
. 1 # Definition List for Singly-linked. 2 # class ListNode: . 3 # DEF the __init __ (Self, X): . 4 # self.val = X . 5 # self.next = None . 6 . 7 class Solution: . 8 DEF mergeTwoLists (Self, L1 : ListNode, L2: ListNode) -> ListNode: . 9 Curr = ListNode head = (0) # Curr showing the operation pointer, head showing the head of the linked list 10 the while L1 and L2: # If they are not empty . 11 IF l1.val < l2.val: 12 = curr.next L1 13 L1 = l1.next 14 the else : 15 curr.next = L2 16 L2 = l2.next 17 curr = curr.next # every cycle should let curr while moving backward a 18 curr. L1 = Next or L2 # Finally, the remaining non-null linked list to the back . 19 20 is return head.next
Source: stay button (LeetCode)
Link: https: //leetcode-cn.com/problems/merge-two-sorted-lists
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.