Tiger-delete duplicate nodes in sorted linked list-python

Subject: 1-1-2-3-3-3-4-5-6-6-7, return to 2-4-5-7 after deleting duplicate nodes

def delete_duplicate(head):
    h = Node(0)
    k = h
    p = head
    if not p or not p.next:
        return head
    q = p.next
    if p.val!=q.val:
        k.next = p
        k = k.next
    while q.next:
        if q.val!=p.val and q.val!=q.next.val:
            k.next = q
            k = k.next
        p = q
        q = q.next
    if p.val!=q.val:
        k.next = q
        k = k.next
    k.next = None

    return h.next

  Note: The judging standard for non-duplicate nodes is different from the front and back nodes. Note that the head node and the tail node must be judged separately.

Guess you like

Origin blog.csdn.net/wh672843916/article/details/105503754