美文网首页
LeetCode 21. Merge Two Sorted Li

LeetCode 21. Merge Two Sorted Li

作者: 洛丽塔的云裳 | 来源:发表于2020-04-04 23:25 被阅读0次

    0. 题目

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

    Example:

    Input: 1->2->4, 1->3->4
    Output: 1->1->2->3->4->4

    1. C++版本

     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            ListNode* newHead = new ListNode(-1);
            ListNode* curr = newHead;
            ListNode* p = l1; ListNode* q = l2;
            while (p && q) {
                if (p->val <= q->val){
                    curr->next = p;
                    curr = curr->next;
                    p = p->next;
                }
                else {
                    curr->next = q;
                    curr = curr->next;
                    q = q->next;
                }
            }
            while (p) {curr->next = p; curr = curr->next; p = p->next;}
            while (q) {curr->next = q; curr = curr->next; q = q->next;}
            return newHead->next;
        }
    

    优化版本:引自https://leetcode.com/problems/merge-two-sorted-lists/discuss/9714/14-line-clean-C%2B%2B-Solution

     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            ListNode* newHead = new ListNode(-1);
            ListNode* curr = newHead;
            while (l1 && l2) {
                if (l1->val <= l2->val){
                    curr->next = l1;
                    l1 = l1->next;
                }
                else {
                    curr->next = l2;
                    l2 = l2->next;
                }
                curr = curr->next;
            }
            curr->next = l1 ? l1 : l2;
            return newHead->next;
        }
    

    2. python版本

      def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            newHead = ListNode(-1)
            curr, p , q = newHead, l1, l2
            while p and q:
                if p.val <= q.val:
                    curr.next = p
                    curr = curr.next
                    p = p.next
                else:
                    curr.next = q
                    curr = curr.next
                    q = q.next
            while p:
                    curr.next = p
                    curr = curr.next
                    p = p.next
            while q:
                    curr.next = q
                    curr = curr.next
                    q = q.next
            return newHead.next
    

    看下比较更加pythonic的解法,引自 https://leetcode.com/problems/merge-two-sorted-lists/discuss/9735/Python-solutions-(iteratively-recursively-iteratively-in-place).

        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            dummy = curr = ListNode(-1)
            while l1 and l2:
                if l1.val <= l2.val:
                    curr.next = l1
                    l1 = l1.next
                else:
                    curr.next = l2
                    l2 = l2.next
                curr = curr.next
            curr.next = l1 or l2
            return dummy.next
    

    相关文章

      网友评论

          本文标题:LeetCode 21. Merge Two Sorted Li

          本文链接:https://www.haomeiwen.com/subject/iwhsphtx.html