Insertion Sort List LeetCode solution in JavaScript
1/**2* Definition for singly-linked list.3* function ListNode(val, next) {4* this.val = (val===undefined ? 0 : val)5* this.next = (next===undefined ? null : next)6* }7*/8/**9* @param {ListNode} head10* @return {ListNode}11*/12var insertionSortList = function (head) {13let ptr = head;1415while (ptr.next !== null) {16if (ptr.val <= ptr.next.val) ptr = ptr.next;17else {18let temp = ptr.next;19ptr.next = ptr.next.next;2021if (temp.val < head.val) {22temp.next = head;23head = temp;24} else {25let ptr2 = head;26while (ptr2.next != null && temp.val >= ptr2.next.val) {27ptr2 = ptr2.next;28}29temp.next = ptr2.next;30ptr2.next = temp;31}32}33}34return head;35};