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

Topic describes

There is a linked list in ascending order. Given the head node of the linked list, remove all duplicate elements so that each element appears only once. Returns a linked list of results, also in ascending order.

Example 1:

Input: head = [1,2]

LeetCode link: leetcode-cn.com/problems/re…

Their thinking

  1. A linked list is an ordered linked list. The same nodes are continuous
  2. When the value of the current node is equal to the value of the next node, change next of the current node to next next node
  3. Handle boundary conditions, return head if head is empty

Code implementation

var deleteDuplicates = function(head) { if (! Head) return head let curr = head while(curr.next) {if (curr.val === curr.next-val) {// If (curr.val === curr.next-val) { Curr.next = curr.next. Next} else {curr = curr.next}} return head};Copy the code

If there are mistakes welcome to point out, welcome to discuss together!