Remove Duplicates From Linked List

refer to: https://www.algoexpert.io/questions/Remove%20Duplicates%20From%20Linked%20List

Problem Statement

Remove Duplicates From Linked List

 

 Analysis

Remove Duplicates From Linked List

 

 Code

# This is an input class. Do not edit.
class LinkedList:
    def __init__(self, value):
        self.value = value
        self.next = None


def removeDuplicatesFromLinkedList(linkedList):
    currentNode = linkedList
    while currentNode is not None:
        nextDistinctNode = currentNode.next
        while nextDistinctNode is not None and nextDistinctNode.value == currentNode.value:
            nextDistinctNode = nextDistinctNode.next
            
        currentNode.next = nextDistinctNode# the first unique value(exclude the head node) found. let the head point to it
        currentNode = nextDistinctNode#update the current node
        
    return linkedList
        
            

Time and Space complexity

Remove Duplicates From Linked List

 

 

 
上一篇:关于drop_duplicates的两种用法


下一篇:26. Remove Duplicates from Sorted Array