Deletes duplicate nodes in a sort linked list

To give you a sorted list, delete the duplicate nodes in the list and return to the deleted list

The problem solving code

If the current node is equal to the next one, delete the next one and continue to traverse the list

var deleteDuplicates = function(head) {
  if(! head)return null;
  let p = head;
  while (p.next) {
    if(p.val === p.next.val) {
      p.next = p.next.next; // If the current node has the same value as the next node, delete the next node
    }else {
      p = p.next; // otherwise p goes back a step and continues until the list is empty}}return head;
};
Copy the code