leetcode之Insertion Sort List

来源:互联网 发布:网页展示 excel数据 编辑:程序博客网 时间:2024/05/20 22:35

问题描述如下:

Sort a linked list using insertion sort.

问题链接

cpp代码如下:

class Solution {public:    ListNode *insertionSortList(ListNode *head) {        if(head==NULL||head->next==NULL)return head;        ListNode node(0);        node.next=head;        ListNode* p=head->next,*r=head;        while(p){            ListNode* q=&node;            for(;q->next!=p&&q->next->val<p->val;q=q->next);            if(q->next==p){                r=p;                p=p->next;            }            else{                r->next=p->next;;                p->next=q->next;                q->next=p;                p=r->next;            }        }        return node.next;    }};


0 0
原创粉丝点击