Leetcode-insertion sort list-List Insert sort-list operations

Source: Internet
Author: User

https://oj.leetcode.com/problems/insertion-sort-list/

Insert sort to assume [0,i] is already an ordered array, the next step from [I,n] to find the smallest element to exchange to I. Use the pointer to simulate the process. Is the operation of some trouble.

Each time [Head,p] is an already ordered array, the next time from [P,tail] Find the smallest node R and its forward node L. Insert it between the lp->p. and notice whether this node is a boundary condition such as p.

/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *}; */Const int INF=2147483647;CLASS Solution {Public:listnode *insertionsortlist (ListNode *head) {if (head==null        ) return NULL;        ListNode *p=head;        ListNode *lp=null;        ListNode *h=head;            while (p!=null) {int minv=inf;            ListNode *l,*r=p;            ListNode *LQ=LP;            ListNode *q=p;                    while (Q!=null) {if (Q->VAL<MINV) {minv=q->val;                    L=LQ;                r=q;                } lq=q;            q=q->next;                } if (r==p) {lp=p;                p=p->next;            Continue            } if (L!=null) l->next=r->next;            r->next=p;            if (lp==null) h=r;            else lp->next=r;        Lp=r;        }return h; }};

  

Leetcode-insertion sort list-List Insert sort-list operations

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.