83.删除排序链表中的重复元素
- 给定一个已排序的链表的头head,删除所有重复的元素,使每个元素只出现一次。返回已排序的链表。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: Optional[ListNode]
:rtype: Optional[ListNode]
"""
current = head
# 创建指针current指向头节点
while current and current.next:
# 遍历链表
if current.val == current.next.val:
current.next = current.next.next
else:
current = current.next
return head
- 时间复杂度: O(n),n为链表长度
- 空间复杂度: O(1)
1996

被折叠的 条评论
为什么被折叠?



