83. 删除排序链表中的重复元素
83. 删除排序链表中的重复元素
题意
删除排序链表中的重复元素
解题思路
题目已经限定了是排好序的链表,那么就需要特殊处理一下可能会出现连续的情况,找到起始结点和结束结点删除即可;
实现
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
temp = head
while temp:
start = temp.next
# 找重复的值的结束位置
while start and start.val == temp.val:
start = start.next
temp.next = start
temp = temp.next
return head

关注公众号:数据结构与算法那些事儿,每天一篇数据结构与算法