Remove Duplicates from Sorted List II Solutions in Go
Number 82
Difficulty Medium
Acceptance 36.9%
Link LeetCode
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport ("github.com/halfrost/LeetCode-Go/structures")// ListNode definetype ListNode = structures.ListNode/*** Definition for singly-linked list.* type ListNode struct {* Val int* Next *ListNode* }*/func deleteDuplicates1(head *ListNode) *ListNode {if head == nil {return nil}if head.Next == nil {return head}newHead := &ListNode{Next: head, Val: -999999}cur := newHeadlast := newHeadfront := headfor front.Next != nil {if front.Val == cur.Val {// fmt.Printf("相同节点front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val)front = front.Nextcontinue} else {if cur.Next != front {// fmt.Printf("删除重复节点front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val)last.Next = frontif front.Next != nil && front.Next.Val != front.Val {last = front}cur = frontfront = front.Next} else {// fmt.Printf("常规循环前front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val)last = curcur = cur.Nextfront = front.Next// fmt.Printf("常规循环后front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val)}}}if front.Val == cur.Val {// fmt.Printf("相同节点front = %v | cur = %v | last = %v\n", front.Val, cur.Val, last.Val)last.Next = nil} else {if cur.Next != front {last.Next = front}}return newHead.Next}func deleteDuplicates2(head *ListNode) *ListNode {if head == nil {return nil}if head.Next != nil && head.Val == head.Next.Val {for head.Next != nil && head.Val == head.Next.Val {head = head.Next}return deleteDuplicates(head.Next)}head.Next = deleteDuplicates(head.Next)return head}func deleteDuplicates(head *ListNode) *ListNode {cur := headif head == nil {return nil}if head.Next == nil {return head}for cur.Next != nil {if cur.Next.Val == cur.Val {cur.Next = cur.Next.Next} else {cur = cur.Next}}return head}// 双循环简单解法 O(n*m)func deleteDuplicates3(head *ListNode) *ListNode {if head == nil {return head}nilNode := &ListNode{Val: 0, Next: head}head = nilNodelastVal := 0for head.Next != nil && head.Next.Next != nil {if head.Next.Val == head.Next.Next.Val {lastVal = head.Next.Valfor head.Next != nil && lastVal == head.Next.Val {head.Next = head.Next.Next}} else {head = head.Next}}return nilNode.Next}// 双指针+删除标志位,单循环解法 O(n)func deleteDuplicates4(head *ListNode) *ListNode {if head == nil || head.Next == nil {return head}nilNode := &ListNode{Val: 0, Next: head}// 上次遍历有删除操作的标志位lastIsDel := false// 虚拟空结点head = nilNode// 前后指针用于判断pre, back := head.Next, head.Next.Next// 每次只删除前面的一个重复的元素,留一个用于下次遍历判重// pre, back 指针的更新位置和值比较重要和巧妙for head.Next != nil && head.Next.Next != nil {if pre.Val != back.Val && lastIsDel {head.Next = head.Next.Nextpre, back = head.Next, head.Next.NextlastIsDel = falsecontinue}if pre.Val == back.Val {head.Next = head.Next.Nextpre, back = head.Next, head.Next.NextlastIsDel = true} else {head = head.Nextpre, back = head.Next, head.Next.NextlastIsDel = false}}// 处理 [1,1] 这种删除还剩一个的情况if lastIsDel && head.Next != nil {head.Next = nil}return nilNode.Next}