力扣之删除排序链表中的重复元素——83

问题

力扣之删除排序链表中的重复元素——83_第1张图片

解答

时间复杂度 :O(n)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        p = head
        if p==None or p.next == None:
            return head
        while p.next:
            if p.val == p.next.val:
                p.next = p.next.next
            else:
                p = p.next
        return head

调试结果
力扣之删除排序链表中的重复元素——83_第2张图片

你可能感兴趣的:(力扣之删除排序链表中的重复元素——83)