"Leetcode Problem Solving Report" single-linked list sorting problems

Source: Internet
Author: User

leetcode Merge Sorted Lists

Problem description

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

Analysis and Solution

There is not much explanation for this problem, direct merging; look at the code:

1 /**2 * Definition for singly-linked list.3 * struct ListNode4  * {5 * int val;6 * ListNode *next;7 * ListNode (int x): Val (x), Next (NULL) {}8  * };9  */Ten classSolution One { A  Public: -listnode* mergetwolists (listnode* L1, listnode*L2) -     { theListNode Dummy (-1);  -ListNode *p = &dummy; -          while(L1 &&L2) -         { +             if(L1->val < l2->val) -             { +P->next =L1; AL1 = l1->Next; at             } -             Else  -             { -P->next =L2; -L2 = l2->Next; -             } inp = p->Next; -         } toP->next = (L1! = NULL)?L1:l2; +         returnDummy.next; -     } the};
View Code Leetcode-Merge k Sorted Lists

Problem description

Merge K sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Analysis and Solution

According to the idea of merging. The reference code is as follows:

1 classSolution2 {3 Private:4listnode* mergetwolists (listnode* L1, listnode*L2)5     {6ListNode Dummy (-1); 7ListNode *p = &dummy;8          while(L1 &&L2)9         {Ten             if(L1->val < l2->val) One             { AP->next =L1; -L1 = l1->Next; -             } the             Else  -             { -P->next =L2; -L2 = l2->Next; +             } -p = p->Next; +         } AP->next = (L1! = NULL)?L1:l2; at         returnDummy.next; -     } -  Public: -listnode* mergeklists (vector<listnode*>&lists) -     { -         intn =lists.size (), I, J; in         if(n = =0)returnNULL; -          while(N >1) to         { +              for(i =0, j = N1; I < J; i++, j--) -             { theLists[i] =mergetwolists (Lists[i], lists[j]); *             } $n = (n+1) /2; Panax Notoginseng         } -         returnlists[0];  the     } +};
View Code leetcode 147 insertion Sort List

Problem description

Sort a linked list using insertion sort.

Analysis and Solution

To insert a sort, refer to the following code:

1 classSolution2 {3  Public:4listnode* Insertionsortlist (listnode*head)5     {6         if(!head | |!head->next)returnhead;7ListNode Dummy (-1); Dummy.next =head;8ListNode *p = &dummy, *q = head, *pe =head;9          while(q)Ten         { One             if(Q->val >= pe->val) A             { -PE =Q; -Q = q->Next; the                 Continue;  -             } -              for(p = &dummy; P->next! = q; p = p->next) -                 if(P->next->val > Q->val) Break; +Pe->next = q->Next; -Q->next = p->Next; +P->next =Q; AQ = pe->Next; at         } -         returnDummy.next; -     } -};
View Code Leetcode 148 Sort List

Problem description

Sort a linked list in O (NLOGN) time using constant space complexity.

Analysis and Solution

Merge sort ideas, recursive implementation, the reference code is as follows:

1 classSolution2 {3  Public:4listnode* Sortlist (listnode*head)5     {6         if(!head | |!head->next)returnhead;7ListNode *fast = head, *slow =head;8          while(Fast->next && fast->next->next)9         {TenFast = Fast->next->Next; Oneslow = slow->Next; A         } -Fast = Slow->Next; -Slow->next =NULL; the         returnMerge (Sortlist (head), sortlist (Fast)); -     } - Private: -listnode* merge (ListNode *l1, ListNode *L2) +     { -ListNode Dummy (0);  +ListNode *p = &dummy; A          while(L1! = NULL && L2! =NULL) at         { -             if(L1->val < l2->val) -             { -P->next =L1; -L1 = l1->Next; -             } in             Else  -             { toP->next =L2; +L2 = l2->Next; -             } thep = p->Next; *         } $P->next = (L1! = NULL)?L1:l2;Panax Notoginseng         returnDummy.next; -     } the};
View Code

"Leetcode Problem Solving Report" single-linked list sorting problems

Related Article

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.