删除排序链表中的重复元素 – Leetcode – 82

huangapple go评论91阅读模式
英文:

Remove Duplicates from Sorted List - Leetcode -82

问题

我已经考虑了三种情况的解决方案:

  • 情况1:如果数组形式为[1,2,3,4,5]
  • 情况2:如果数组形式为[1,1,1,2,3,4,5]
  • 情况3:如果数组形式为[1,2,3,3,3,3,4,5]

我的Go语言解决方案如下:

type ListNode struct {
     Val int
     Next *ListNode
}
func deleteDuplicates(head *ListNode) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }
    current := head
    var prev *ListNode
    for current.Next != nil {
        if current.Val != current.Next.Val {   // (情况1)
            prev = current
            current = current.Next
        } else if current == head && current.Val == current.Next.Val {  //(情况2)
            current = current.Next.Next
            head = current
        } else if current != head && current.Val == current.Next.Val { //(情况3)
            for current.Val == current.Next.Val {
                current = current.Next
            }
            temp := current.Next
            prev = temp
            current = prev.Next
        }
    }
    return head 
}  

我在情况3中遇到了问题?我不知道我做错了什么。

英文:

I have considered 3 cases for the solution as:

  • CASE 1 : If array is in form [1,2,3,4,5]
  • CASE 2 : If array is in form [1,1,1,2,3,4,5]
  • CASE 3 : If array is in form [1,2,3,3,3,3,4,5]

My solution in Go.

type ListNode struct {
     Val int
     Next *ListNode
}
func deleteDuplicates(head *ListNode) *ListNode {
    if head == nil || head.Next ==nil{
        return head
    }
    current := head
    var prev *ListNode
    for current.Next != nil {
        if current.Val != current.Next.Val{   // (CASE-1)
            prev = current
            current = current.Next
        } else if current ==head && current.Val == current.Next.Val {  //(CASE-2)
            current = current.Next.Next
            head = current
        } else if current != head && current.Val == current.Next.Val { //(CASE-3)
            for current.Val == current.Next.Val{
                current = current.Next
            }
            temp := current.Next
            prev = temp
            current = prev.Next
        }
    }
    return head 
}  

I am having a problem in CASE-3 ? I don't know what I am doing wrong.

答案1

得分: 1

你正在通过分别解决每种情况来使解决方案过于复杂化。如果你要去重:

current := head
for current.Next != nil {
   if current.Next.Val == current.Val {
      // 删除重复节点,保持在同一节点上
      current.Next = current.Next.Next
   } else {
     // 前进到下一个节点
      current = current.Next
   }
}

如果你要删除所有重复值的实例:

current := head
var prev *ListNode
for current != nil {
  trc := current
  // 找到下一个具有不同值的节点
  for trc != nil {
     if trc.Val == current.Val {
        trc = trc.Next
     } else {
         break
     }
  }

  // 如果 trc==nil,则剩余的所有值都相同
  // 如果 prev 也是 nil,则列表中的所有值都相同

  if trc == nil {
     if prev == nil {
        // 列表中的所有值都相同
     } else {
        prev.Next = nil
        current = nil
     }
  } else if trc == current {
    // 不是重复的条目
    prev = current
    current = current.Next
  } else {
    if prev != nil {
       prev.Next = trc.Next
    } else {
       // 你需要设置 head = trc.Next
    }
    current = trc.Next
  }
}
英文:

You are overcomplicating your solution by solving each case separately. If you are deduplicating:

current:=head
for current.Next!=nil {
   if current.Next.Val==current.Val {
      // Remove the duplicate node, stay on the same node
      curent.Next=current.Next.Next
   } else {
     // Advance to the next node
      current=current.Next
   }
}

If you are removing all instances of duplicated values:

current:=head
var prev *ListNode
for current!=nil {
  trc:=current
  // Find the next node with a different value
  for trc!=nil {
     if trc.Val==current.Val {
        trc=trc.Next
     } else {
         break
     }
  }

  // if trc==nil, all remaining values are the same
  // if prev is also nil, all values in the list are the same

  if trc==nil {
     if prev==nil {
        // All values in the list are the same
     } else {
        prev.Next=nil
        current=nil
     }
  } else if trc==current {
    // Not a duplicate entry
    prev=current
    current=current.Next
  } else {
    if prev!=nil {
       prev.Next=trc.Next
    } else {
       // you need to set head =  trc.Next
    }
    current=trc.Next
  }
}

答案2

得分: 0

乍一看似乎很简单,但仔细研究后,问题实际上非常恼人。你必须考虑三种情况。元素可以在前面重复。除非找到单个元素,否则必须搜索头部。其他两种情况可以通过一个算法解决,因为将节点的下一个设置为nil并不重要。

package main

import "fmt"

type LL struct {
	Val  int
	Next *LL
}

func removeDuplicates(h *LL) (res *LL) {
	current := h
	var prev *LL

	// 令人讨厌的情况
	if current.Next == nil {
		return current
	}

	// 更令人讨厌的情况
	if current.Next.Val != current.Val {
		res = current
	}

	for current.Next != nil {
		if current.Next.Val == current.Val {
			current.Next = current.Next.Next
			// 尾部情况 - 最令人讨厌的情况
			if prev != nil && (current.Next == nil || current.Next.Val != current.Val) {
				prev.Next = current.Next
			}
		} else {
			prev = current
			current = current.Next
			// 前面的重复 - 相当令人讨厌
			if res == nil && (current.Next == nil || current.Next.Val != current.Val) {
				res = current
			}
		}
	}

	return
}

func main() {
	l := &LL{1, &LL{1, &LL{2, &LL{2, &LL{3, &LL{4, &LL{4, &LL{5, &LL{6, &LL{6, nil}}}}}}}}}}
	l = removeDuplicates(l)

	current := l
	for current != nil {
		fmt.Print(current.Val)
		current = current.Next
	}

	fmt.Println(l)
}
英文:

At first glance it looks trivial but after looking deeply into it, issue is actually really annoying. You have to take three cases into account. Elements can be repeating in front. Unless you find single element you have to search for head. Other two cases can be solved by one algorithm, as setting next to nil of node does not matter.

package main

import "fmt"

type LL struct {
	Val  int
	Next *LL
}

func removeDuplicates(h *LL) (res *LL) {
	current := h
	var prev *LL

	// annoying
	if current.Next == nil {
		return current
	}

	// even more annoying
	if current.Next.Val != current.Val {
		res = current
	}

	for current.Next != nil {
		if current.Next.Val == current.Val {
			current.Next = current.Next.Next
			// trailing case - most annoying of them all
			if prev != nil && (current.Next == nil || current.Next.Val != current.Val) {
				prev.Next = current.Next
			}
		} else {
			prev = current
			current = current.Next
			// front repetition - decently annoying
			if res == nil && (current.Next == nil || current.Next.Val != current.Val) {
				res = current
			}
		}
	}

	return
}

func main() {
	l := &LL{1, &LL{1, &LL{2, &LL{2, &LL{3, &LL{4, &LL{4, &LL{5, &LL{6, &LL{6, nil}}}}}}}}}}
	l = removeDuplicates(l)

	current := l
	for current != nil {
		fmt.Print(current.Val)
		current = current.Next
	}

	fmt.Println(l)
}

huangapple
  • 本文由 发表于 2021年5月24日 02:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/67663125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定