Delete duplicate-value nodes from a sorted linked list:

"""
 Delete duplicate nodes
 head could be None as well for empty list
 Node is defined as

 class Node(object):

   def __init__(self, data=None, next_node=None):
       self.data = data
       self.next = next_node

 return back the head of the linked list in the below method.
"""

def RemoveDuplicates(head):
    prev = head
    p = head.next

    while p!=None:
        if p.data == prev.data:
            prev.next = p.next
            p = p.next
        else:
            prev = p
            p = p.next

    return head

results matching ""

    No results matching ""