Merge twice Sorted Lists, your code passes through

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.