1
// Please upvote if it helps4
ListNode *sortList(ListNode *head) {5
// If List Contain a Single or 0 Node6
if (head == NULL || head->next == NULL) return head;10
ListNode *fast = head;12
// 2 pointer appraoach / turtle-hare Algorithm (Finding the middle element)13
while (fast != NULL && fast->next != NULL) {15
slow = slow->next; // slow increment by 116
fast = fast->next->next; // fast incremented by 218
temp->next = NULL; // end of first left half20
ListNode *l1 = sortList(head); // left half recursive call21
ListNode *l2 = sortList(slow); // right half recursive call23
return mergelist(l1, l2); // mergelist Function call26
// MergeSort Function O(n*logn)27
ListNode *mergelist(ListNode *l1, ListNode *l2) {28
ListNode *ptr = new ListNode(0);31
while (l1 != NULL && l2 != NULL) {32
if (l1->val <= l2->val) {43
// for unqual length linked list