Title Description: Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.
In fact, the topic is very simple to understand, that is, two orderly linked lists into a new linked list.
1 defmergetwolists (self, L1, L2):2 ifL1 = =None:3 returnL24 elifL2 = =None:5 returnL16 elifL1 = = None andL2 = =None:7 returnNone8 Else:9P1 =L1TenP2 =L2 One ifP1.val >P2.val: AL =P2 -p =P2 -P2 =P2.next the whileP1 andP2: - ifP1.val >P2.val: -P.next =P2 -p =P2 +P2 =P2.next - + Else: AP.next =P1 atp =P1 -P1 =P1.next - - ifP1: - whileP1: -P.next =P1 inp =P1 -P1 =P1.next to returnL + ifP2: - whileP2: theP.next =P2 *p =P2 $P2 =P2.nextPanax Notoginseng returnL - Else: theL =P1 +p =P1 AP1 =P1.next the whileP1 andP2: + ifP1.val >P2.val: -P.next =P2 $p =P2 $P2 =P2.next - - Else: theP.next =P1 -p =P1WuyiP1 =P1.next the - ifP1: Wu whileP1: -P.next =P1 Aboutp =P1 $P1 =P1.next - returnL - ifP2: - whileP2: AP.next =P2 +p =P2 theP2 =P2.next - returnL
Merge twice Sorted Lists, your code passes through