Sort list & insertion sort list (list sorting summary)

Source: Internet
Author: User
Sort list

Sort a linked list inO(NLogN) Time using constant space complexity.

 

Note: Merge Sorting: time O (nlogn) and space O (1). the linked list is split into two parts each time and then merged. Fast sorting (using two pointers)
/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */ ListNode* getMid(ListNode *head) {    ListNode *first, *second, *preFirst;    preFirst = first = second = head;    while(second != NULL) {        preFirst = first;        first = first->next;        if(second->next == NULL) break;        second = second->next->next;    }    preFirst->next = NULL;    return first;}void Merge(ListNode *head1, ListNode *head2) {    if(head1->val > head2->val) {int tem = head1->val;head1->val = head2->val;head2->val = tem;}    ListNode *p = head1;    while(p->next != NULL && head2 != NULL) {        if(p->next->val >= head2->val) {            ListNode *q = p->next;            p->next = head2;            head2 = head2->next;            p->next->next = q;            p = p->next;        } else {            p = p->next;        }    }    if(head2 != NULL)        p->next = head2;} void MergeSort(ListNode *head) {    if(head == NULL || head->next == NULL) return;    ListNode *mid = getMid(head);MergeSort(head);MergeSort(mid);    Merge(head, mid);} class Solution {public:    ListNode *sortList(ListNode *head) {       MergeSort(head);       return head;    }};

 

Insertion Sort list

Sort a linked list using insertion sort.

Note: different from the sequence table, you can start from the beginning each time you locate the insert position.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode *insertionSortList(ListNode *head) {        ListNode *p, *q;        for(q = head; q != NULL; q = q->next) {            for(p = head; p != q; p = p->next) {                if(p->val >= q->val) {                    int tem = p->val;                    p->val = q->val;                    q->val = tem;                }            }        }        return head;    }};

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.