147. Insertion Sort List

来源:互联网 发布:宜人贷网络贷款 编辑:程序博客网 时间:2024/04/26 01:14

Sort a linked list using insertion sort.

链表的插入排序:

class Solution {public:    ListNode* insertionSortList(ListNode* head) {        if(head==NULL)return NULL;        ListNode *sort_list=new ListNode(0);        while(head!=NULL){            ListNode *tmp=head->next;            ListNode *cur=sort_list;            while(cur->next!=NULL&&cur->next->val<head->val){                cur=cur->next;            }            head->next=cur->next;            cur->next=head;            head=tmp;        }        return sort_list->next;    }};
0 0
原创粉丝点击