Remove sorts the list of repeating elements --python

Problem Description

Given a sorted list, delete all the duplicate elements, so that each element occurs only once.

Example 1:

Input: 1-> 1-> 2
Output: 1-> 2
Example 2:

Input: 1-> 1-> 2-> 3-> 3
Output: 1-> 2-> 3

Double pointer Code

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

operation result

Here Insert Picture Description

Released eight original articles · won praise 1 · views 155

Guess you like

Origin blog.csdn.net/fromatlove/article/details/104737649