Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
var deleteDuplicates = function (head) { if (head == null) { return head; } let cur = head; let headnode = head; let pre = null; while (cur != null && cur.next != null) { if (cur.val == cur.next.val) { let next = cur.next; while (next != null && next.val == cur.val) { next = next.next; } if (pre == null) { headnode = next; cur = next; } else { pre.next = next; cur = next; } } else { pre = cur; cur = cur.next; } } return headnode; };