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

问题

在这里插入图片描述

解答

时间复杂度 :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
发布了25 篇原创文章 · 获赞 8 · 访问量 873

猜你喜欢

转载自blog.csdn.net/weixin_44617944/article/details/104727337
今日推荐