"Leetcode with Python" Merge two Sorted Lists__python

Source: Internet
Author: User
Blog Domain name: Http://www.xnerv.wang
Original title page: https://oj.leetcode.com/problems/merge-two-sorted-lists/
Topic Type:
Difficulty Evaluation: ★
This article address: http://blog.csdn.net/nerv3x3/article/details/3465680

Merge two sorted linked lists and return it as a new list.


Combining two sequential lists, common data structure exercises during college.


Class Solution:
    # @param two listnodes
    # @return a listnode
    def mergetwolists (self, L1, L2):
        If None = L1 And none = L2: Return
            none
        elif none = L1: Return
            L2
        elif none = L2: Return
            l1 new_list

        = List Node (0)
        cur = new_list
        node1 = L1
        node2 = L2 while
        None!= Node1 and none!= Node2:
            if Node1.val < node2.val:
                cur.next = node1
                Node1 = node1.next
            else:
                cur.next = node2 Node2
                = node2.next< C20/>cur = Cur.next
        if None!= node1:
            cur.next = Node1
        else:
            cur.next = node2 return

        new_ List.next

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.