Insertion Sort List

ListNode insertionSortList(ListNode head) {

if(head==NULL || head->next==NULL) return head;

ListNode *p=head;

ListNode temp, h1=new ListNode(0), *helper;

while(p){

helper=h1;

while(helper->next&&helper->next->val<p->val){

helper=helper->next;

}

temp=p->next;

p->next=helper->next;

helper->next=p;

p=temp;

}

return h1->next;

}

Last updated

Was this helpful?