“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:
- First, the linked list is judged to be empty or has only one node, and the original linked list is directly returned
- Since the head node can also be deleted, create a virtual head node
vhead
Facilitate the final return results - Initialize the
pre
The pointer points to the virtual head nodevhead
- Initialize the
cur
The pointer points to the head nodehead
- Initialize the
next
Pointer tohead.next
- Walk through the list, judge
next
Pointer to whether the node value is equal tocur
The pointer points to the node value. If the value is equal, duplicate elements are presentnext
The pointer goes backwards until it finds the first one that does not equalcur
The nodes of the - will
pre.next
Point to thenext
To deletecur
And the effect of repeating elements - update
cur = next
next = next? next.next:null
- When the list traversal is complete, return
vhead.next
Can 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!