21.merge Two sorted lists [two ordered lists, integrated into an ordered list]

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.

Examples of
Here Insert Picture Description
ideas

  • Iteration

Application head node, traversal of the node two lists, each node to find the minimum, the new list on the back, and then a smaller junction point where the next list node and the node before large compared

  • Recursion
  • Each call, first select a smaller nodes, and then sorted behind it, returns the smaller nodes to the beginning of the sorted list

answer

  • python
*迭代*
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        head = ListNode(0)
        cur = head
        while l1 and l2:
            if l1.val<l2.val:
                cur.next=l1
                l1=l1.next
            else:
                cur.next=l2
                l2=l2.next
            cur=cur.next
            
        cur.next=l1 or l2
        # 取代 if l1:cur.next=l1   if l1:cur.next=l2
        return head.next
*递归*
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        #l1 or l2为空时
        if l1 is None or l2 is None:
            return l1 or l2 
        #比较两个链表的当下结点,选出较小的,并递归得到剩下的结点中最小的。两个结点一个单位
        if l1.val <l2.val:
            l1.next = self.mergeTwoLists(l1.next,l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1,l2.next)
            return l2
  • java
#boss创公司,boss的分身clone,一个市场,一个市场的开拓
#给两个有序链表排序,两个老总亲自下场,老总总是向前走,停在所站位置比较后,各自最小的地方(走过的不算了)
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        boss = ListNode(-1)#办理公司
        clone = boss
        while l1 and l2:#l1 and l2都不为None
            if l1.val<l2.val:
                #其实只是获取了该结点,同ListNode(l1.val),反正比较后小的结点就不要了,直接下一个结点了
                clone.next = l1 
                l1 = l1.next
            else:
                clone.next = l2
                l2 = l2.next
            clone = clone.next #开拓下一个市场
            
        clone.next = l1 or l2 # 从while中出来/或者本来就是,l1 or l2为None
        return boss.next #子公司串
    
   
#递归
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        #l1 or l2为空时
        if l1 is None or l2 is None:
            return l1 or l2 
        #比较两个链表的当下结点,选出较小的,并递归得到剩下的结点中最小的。两个结点一个单位
        if l1.val <l2.val:
            l1.next = self.mergeTwoLists(l1.next,l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1,l2.next)
            return l2
  • c++
*迭代*
   ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* head = new ListNode(0);
        ListNode* cur = head;
        
        while (l1 && l2)
        {
            if (l1->val<l2->val)
            {
                cur->next = l1;
                l1 = l1->next;
            }
            else
            {
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        cur->next = l1?l1:l2;
        /* 取代
        if (l1) cur->next = l1;
        if (l2) cur->next = l2;
        */
        return head->next;
    }
*递归*
 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if (!l1)
            return l2;
        if (!l2)
            return l1;
        if (l1->val<l2->val)
        {
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;//
        }
        else
        {
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
    }
Published 78 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/puspos/article/details/103070154