[C + +] leetcode:125 Sort list (merge sort lists)

Source: Internet
Author: User

Topic:Sort A linked list inO(NLogN) time using constant space complexity.

idea: the topic asks us to use constant space complexity, the time complexity is O (Nlog (n)). To meet this time complexity there are quick sort, merge sort, heap sort. Insert sort time complexity is O (n^2). The two-way list is suitable for quick row, the heap sort can also be used in the linked list, the single necklace table is suitable for the merge sort. We use the idea of merge sort to complete the sort of list.

The first is to use a fast and slow double pointer to find the middle of the list, and then divided into the front and back of the recursive merge sort, and finally merged.

Attention:

1. The method to find the middle node is a common method, which should be used skillfully.

The linked list is divided into two parts: double hands to find the middle node        listnode* fast = head;        listnode* slow = head;        while (Fast->next = null && fast->next->next! = null)        {            slow = slow->next;            Fast = fast->next->next;        }
2. The data structure of the linked list is how to realize the merging method. Define a combined linked list and put the appropriate nodes in. Thoughts and arrays are the same, except that the list operation is not the same.

Complexity: If we consider recursive stack space here, the spatial complexity is O (LG (n))

AC Code:

/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *}; */class Solution {Public:listnode *sortlist (ListNode *head) {if (head = = NULL | | head->next = NULL) return        Head        The linked list is divided into two parts: double hands to find the middle node listnode* fast = head;        listnode* slow = head;            while (Fast->next = null && fast->next->next! = null) {slow = slow->next;        Fast = fast->next->next;        } listnode* head2 = slow->next;        Slow->next = NULL;        listnode* head1 = head;   Head1 = Sortlist (HEAD1);   The first half of the merge sort head2 = Sortlist (head2);    Second half merge sort return merge (Head1, head2);        }private:listnode* merge (listnode* H1, listnode* h2) {listnode* dummyhead = new ListNode (0);                listnode* mlist = Dummyhead; while (H1 = null && h2! = null) {if (H1->val < = h2->val) {mlist->next = h1;            H1 = h1->next;                } else {mlist->next = H2;            H2 = h2->next;        } mlist = mlist->next;        } if (H1! = NULL) {mlist->next = h1;        } if (H2! = NULL) {mlist->next = H2;    } return dummyhead->next; }};

We can also not use recursive method, bottom-up non-recursive version of the merge sort, spatial complexity is constant. Take a look at this blog post: Sort list.
Sorting is a very common and basic topic in interviews, and we need to be familiar with a variety of common sorting algorithms. In particular, the principle of the algorithm, many topics although not directly to the implementation of the sequencing, but will use the ideas, such as the classic TOPK problem, the use of the principle of the fast line. As mentioned in this article, you can look at: Median of two Sorted Arrays. So we should pay more attention to the principle of the sorting algorithm.

[C + +] leetcode:125 Sort list (merge sort lists)

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.