[C + +] leetcode:126 Insertion Sort list (Insert sort lists)

Source: Internet
Author: User

Title:sort a linked list using insertion Sort.


idea: The topic requires that we use the insertion sort to achieve the list sort. We build a list of currently ordered lists, and then maintain a variable that points to the next node in the list. Use the variable cur to represent the node that is currently being inserted, each time the loop finds the corresponding position of the CUR node in the current sorted list, then inserts it in, then points to the next node in the original linked list, continues the insertion process until all the nodes of the original list have been completed, and the results are sorted after n iterations.

Complexity: Time O (n^2) space O (1)

Attention:

1. Maintain a list of currently sorted lists and construct a virtual head node. We always insert the next node in the original list into the appropriate position in the new linked list.

listnode* dummyhead = new ListNode (0); listnode* pre = Dummyhead;
Find the first node that is greater than cur->val in the currently ranked new list while            (pre->next! = NULL && pre->next->val <= cur->val)            {                pre = pre->next;            }                        The value of the next node of the current pre is greater than the value of cur, and cur is inserted after the pre            Cur->next = pre->next;            Pre->next = cur;
2. Maintain a node next, which represents the next node to be inserted in the original list. After a round of iterations, we set the cur to next. So we don't have to judge the next node by its length.

listnode* next = cur->next;  Maintain the next node of the list
cur = next;   Cur pointing to the next node in the original list
3. Each time we need to re-loop from the table header of the sorted new list, find the appropriate location to insert the node.

Pre = Dummyhead;             Reset the pre to new linked header start

AC Code:
/** * 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) {if (head = = NULL | | head->next = NULL        ) return head;        listnode* dummyhead = new ListNode (0);        listnode* pre = Dummyhead;                listnode* cur = head;  while (cur! = NULL) {listnode* next = cur->next;             Maintain the next node of the list pre = Dummyhead; Reset the pre to a new linked header start//Find the first node greater than Cur->val in the current row of new lists while (Pre->next! = NULL && P            Re->next->val <= cur->val) {pre = pre->next;            }//The value of the next node of the current pre is greater than the value of cur, inserting cur into the pre Cur->next = pre->next;            Pre->next = cur;   cur = next;    Cur point to the next node of the original list} return dummyhead->next; }};

This problem, at first I also have to consider inserting in the original list, and the operation of the array is a bit like, because we want to insert the current element in the well-ordered list of the appropriate position, so that we need not only the size, but also to ensure that the pre node can not cross the current node, There is no guarantee that our insertion is correct. For example {3,4,1}, the first iteration Pre->next stop at 3, cur in 4, we can not plug 4 to 3 front. So for the linked list, the operation in the original list is more complicated. You can avoid many judgments by creating a new sorted list for insert operations.




[C + +] leetcode:126 Insertion Sort list (Insert 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.