“This is the 23rd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

[B] [C] [D]

There is a linked list in ascending order, I give you the head node of the linked list, please delete all the nodes in the linked list that have the same number of digits, only keep the original linked list does not have the same number of digits.

Returns a linked list of results, also in ascending order.

Example 1:

Input: head = [1,2,3,3,4,4,5]Copy the code

Example 2:

Input: head = [1,1,1,2,3]Copy the code

Tip:

  • The number of nodes in the linked list is in the range[0, 300] 内
  • -100 <= Node.val <= 100
  • The subject data ensures that the linked list is sorted in ascending order

83. Delete duplicate elements from a sorted list, but add 🤏 to the list

We are asked to delete duplicate elements from an ordered list

That is, when an element with the same value appears more than once, it should be deleted

And since the list is ordered, the elements with the same value must be joined together

So we can tell if there are duplicate elements as we walk through the list

If there are duplicate elements, they are removed to the end of the list

What about deleting a node from a linked list?

For example, if there is a linked list 1=>2=>3=> NULL, if you want to delete node 2 in the linked list, you only need to point the pointer of next of node 1 in the linked list to node 3 to achieve the effect of deleting node 2 in the linked list

So with that in mind, how do we solve this problem

The solution is as follows:

  1. First, the linked list is judged to be empty or has only one node, and the original linked list is directly returned
  2. Since the head node can also be deleted, create a virtual head nodevheadFacilitate the final return results
  3. Initialize thepreThe pointer points to the virtual head nodevhead
  4. Initialize thecurThe pointer points to the head nodehead
  5. Initialize thenextPointer tohead.next
  6. Walk through the list, judgenextPointer to whether the node value is equal tocurThe pointer points to the node value. If the value is equal, duplicate elements are presentnextThe pointer goes backwards until it finds the first one that does not equalcurThe nodes of the
  7. willpre.nextPoint to thenextTo deletecurAnd the effect of repeating elements
  8. updatecur = next next = next? next.next:null
  9. When the list traversal is complete, returnvhead.nextCan be

The overall process is as follows:

The code is as follows:

Var deleteDuplicates = function (the head) {/ /, sentenced to list is empty or only one node, directly back to the original list if (head = = = null | | head. The next = = = null) return the head; Const vhead = new ListNode(0); vhead.next = head; Let pre = vhead, cur = head, next = head.next, tag = false;  While (next && next. Val === cur.val){// Mark the current cur element tag = true; next = next.next; } // If (tag){pre.next = next; cur = next; next = next? next.next:null; tag = false; }else{// if not, go back through the list. cur = cur.next; next = next? next.next:null; } } return vhead.next; };Copy the code

At this point we are done with Leetcode-82 – removing duplicate elements II from the sorted linked list

If you have any questions or suggestions, please leave a comment!