Insertion Sort List

 https://www.interviewbit.com/problems/insertion-sort-list/

ListNode* Solution::insertionSortList(ListNode* A) {
    ListNode* first = A;
    A=A->next;
    ListNode* temp_first = first;
    int temp;
    while (A!=NULL)
    {
      if(temp_first!=A)
      {
        if (temp_first->val >= A->val)
        {
          temp = A->val;
          A->val = temp_first->val;
          temp_first->val = temp;
        }
        else
        {
         temp_first = temp_first->next;
        }
       }
       else
       {
         A = A->next;
         temp_first = first;
        }
    }
return first;
}


 

Comments

Popular posts from this blog

Perfect Peak of Array

Is Rectangle?

Sort array with squares!