Tigers - delete duplicate nodes sorted linked list

Title: 1-1-2-3-3-3-4-5-6-6-7, delete duplicate node returned 2-4-5-7

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 criteria for the node will not be repeated, and before and after the node is not the same, attention head node and tail node to be determined separately.

Guess you like

Origin www.cnblogs.com/kingshine007/p/11518858.html