class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if(!head)return head;
queue<ListNode*>q;
ListNode* p=head;
while(p){
q.push(p);
p=p->next;
}
head=NULL;
while(!q.empty()){
p=head;
ListNode *past_p=NULL;
ListNode* t=q.front();
q.pop();
while(p&&p->val<t->val){
past_p=p;
p=p->next;
}
t->next=p;
if(past_p==NULL)
head=t;
else past_p->next=t;
}
return head;
}
};