七叶笔记 » golang编程 » Golang刷题 Leetcode 83. Remove Duplicates from Sorted List

Golang刷题 Leetcode 83. Remove Duplicates from Sorted List

题目:Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once .

给一个排好序的 链表 ,删除其中重复的元素

思路

简单的循环,删除链表节点的操作

code

func deleteDuplicates(head *ListNode) *ListNode {
cur := head
for cur !=  nil  && cur.Next != nil {
if cur.Next.Val == cur.Val {
cur.Next = cur.Next.Next
} else {
cur = cur.Next
}
}
return head
}
 

更多内容请移步我的repo:

相关文章