NOTE
删除排序链表中的重复元素
删除排序链表中的重复元素的 LeetCode 解题笔记。
这是历史学习笔记,可能存在过时或不完整的理解。
1. 题目描述
给定一个已排序的链表的头 head , 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表
2. 思路
3. 实现
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates(head *ListNode) *ListNode {
dummyHead := &ListNode{}
d := dummyHead
current := head
for current != nil {
next := current.Next
current.Next = nil
d.Next = current
d = d.Next
for next != nil && next.Val == current.Val {
next = next.Next
}
current = next
}
return dummyHead.Next
}
讨论
使用 GitHub 账号参与讨论,评论会保存在 GitHub Issues 中。在 GitHub 查看