PLEASE CORRECT MY CODE

hello @gaganpreetsachdev1

ur logic is incorrect.
swapping data with current node will not ensure sorted list it will only push maximum to the end…
image

here u need to interate while(ins->data data) .

and then u need to inserted cur at that position where this condition fails.
take help from here

ListNode *insertionSortList(ListNode *head)
{
	if (head == NULL || head->next == NULL)
		return head;

	ListNode *p = head->next;
	head->next = NULL;

	while (p != NULL)
	{
		ListNode *pNext = p->next;    /*store the next node to be insert*/
		ListNode *q = head;

		if (p->val < q->val)    /*node p should be the new head*/
		{
			p->next = q;
			head = p;
		}
		else 
		{
			while (q != NULL && q->next != NULL && q->next->val <= p->val)
				q = q->next;
			p->next = q->next;
			q->next = p;
		}

		p = pNext;
	}
	return head;
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.