Insertion Sort List, insertionsortlist

Source: Internet
Author: User

Insertion Sort List, insertionsortlist

Solution:
1. This is the deformation of the sequential insertion of the linked list.
2. Set four pointers: insert, query, pre-insert, and pre-query.

 

1/** 2 * Definition for singly-linked list. 3 * struct ListNode {4 * int val; 5 * struct ListNode * next; 6 *}; 7 */8 struct ListNode * insertionSortList (struct ListNode * head) {9 struct ListNode * insert; 10 struct ListNode * insert_p; 11 struct ListNode * cur; 12 struct ListNode * cur_p; 13 int flag; 14 if (head = NULL) 15 return NULL; 16 insert_p = head; 17 insert = head-> next; 18 while (insert! = NULL) {19 cur = head; 20 cur_p = head; 21 flag = 0; 22 while (cur! = Insert) {23 if (head-> val> insert-> val) {// insert the header node. Note that the switching order is 24 insert_p-> next = insert-> next; 25 insert-> next = head; 26 head = insert; 27 insert = insert_p-> next; 28 flag = 1; 29 break; 30} 31 else if (cur-> val> insert-> val) {32 insert_p-> next = insert-> next; 33 cur_p-> next = insert; 34 insert-> next = cur; 35 insert = insert_p-> next; 36 flag = 1; 37 break; 38} 39 cur_p = cur; 40 cur = cur-> next; 41} 42 if (flag = 0) {// flag is used to check whether the insert operation is performed. if the insert operation has been moved to the next position, no insert operation is required; 44 insert = insert-> next; 45} 46} 47 return head; 48}

 

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.