funcdeleteDuplicates(head *ListNode) *ListNode { if head == nil { return head } slow, fast := head, head for fast != nil { if fast.Val != slow.Val { slow = slow.Next slow.Val = fast.Val } fast = fast.Next } slow.Next = nil// 断开不重复部分和后面的链接 return head }
funcremoveElement(nums []int, val int)int { slow, fast := 0, 0 for fast < len(nums) { if nums[fast] != val { nums[slow] = nums[fast] slow++ } fast++ } return slow }