leetcode83:删除排序链表中重复元素

思想:

由于链表是排序的,只要判断head和head.next的val值是否相等,若相等则head.next = head.next.next 反之head = head.next

为了返回,将head暂存dummy

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

class Solution:
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummy= head 
        
        while head and head.next:
            if head.val == head.next.val:
                head.next = head.next.next
            else:
                head = head.next
        return dummy

猜你喜欢

转载自blog.csdn.net/weixin_43160613/article/details/83048643