Tencent merger leetcode selection of exercises sorted linked list

Merge two ordered lists

topic:

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.
Example:
Input: 1-> 2-> 4, 1-> 3-> 4
Output: 1-> 1-> 2-> 3-> 4-> 4

Ideas:

In view of the sum of the two numbers and a summary of the subject, this is not defined two point two temporary variables list, directly traverse two lists
list 1. Define a lead node
2. traverse two lists l1, l2 , the size of the current node value comparing two lists, results summary points directly into the list, moving backward while the linked list, wherein a cyclic process until the end of the list is NULL.
3. The linked list is not NULL inserted into the result list

Code:

ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    ListNode res(-1);
    ListNode* pRes = &res;
    while (l1 != NULL && l2 != NULL)
    {
        if (l1->val > l2->val)
        {
            pRes->next = l2;
            l2 = l2->next;
        }
        else {
            pRes->next = l1;
            l1 = l1->next;
        }
        pRes = pRes->next;
    }
    if (l1 == NULL)
    {
        pRes->next = l2;
    }
    if (l2 == NULL)
    {
        pRes->next = l1;
    }
    return res.next;
}

to sum up:

With optimized experience on a topic, you can directly omit some thought of intermediate links, direct satisfied with a one-time write code.

K merge sort list

topic

K merge sort list, return the list sorted combined. Please complexity analysis and description of the algorithm.
Example:
Input:
[
  1-> 4-> 5,
  1-> 3-> 4,
  2-> 6
]
Output: 1-> 1-> 2-> 3-> 4-> 4-> 5-> 6

Ideas:

1. The combined list of K is selected to be merged into the list with the two remaining after a linked list then combined synthetic eleven obtain the final result
2. merging the two lists to invoke the function directly subject to complete.

Code:

Conventional ideas:

ListNode* mergeKLists(vector<ListNode*>& lists) {
    ListNode res(-1);
    ListNode* pRes = &res;
    for (size_t i = 0; i < lists.size(); i++)
    {
        pRes->next = mergeTwoLists(lists[i], pRes->next);
    }
    return res.next;
}

Recursive ideas:

ListNode* mergeKLists(vector<ListNode*>& lists) {
    int size = lists.size();
    if (size == 0) 
    {
        return NULL;
    }

    if (size == 1) 
    { 
        return lists[0]; 
    }
    for (int i = 0; i < size / 2; i++)
    {
        lists[i] = mergeTwoLists(lists[i], lists[size - i - 1]);
        lists.pop_back();
    }
    ListNode res(-1);
    res.next = mergeKLists(lists);
    return res.next;
}

to sum up

1. The conventional idea is to merge each list one by one.
2. The recursive idea is to list twenty-two merged into one group, eventually all packets pairwise merge again until combined into one list so far, this is the idea of recursive partition
3. The comparison of the two ideas: the idea of reducing the recursive the number of merger.

Guess you like

Origin www.cnblogs.com/zh20130424/p/12189457.html