03(July) Remove all occurences of duplicates in a linked list

03. Remove All Occurrences of Duplicates in a Linked List

The problem can be found at the following link: Question Link

Problem Description

Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list, and return the head of the modified linked list.

Examples:

Input:

Linked List = 23->28->28->35->49->49->53->53

Output:

23 35

Explanation: The duplicate numbers are 28, 49, and 53, which are removed from the list.

Approach

  1. Initialization:

    • Create a dummy node that points to the head of the linked list to handle edge cases, such as when the head itself is a duplicate.

    • Initialize pointers prev and current, where prev points to the dummy node, and current points to the head.

  2. Iterate Through the List:

    • Traverse the linked list with the current pointer.

    • Use a nested loop to check for consecutive nodes with the same value, marking them as duplicates.

    • If duplicates are found, skip all nodes with the duplicate value by adjusting the next pointer of prev to point to the node after current.

    • If no duplicates are found, move the prev pointer to the next node.

    • Move the current pointer to the next node in all cases.

  3. Return:

    • Return the node after the dummy node, which is the new head of the modified linked list.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), where n is the number of nodes in the linked list. This is because we traverse the list once.

  • Expected Auxiliary Space Complexity: O(1), as we use a constant amount of additional space.

Code (C++)

Code (Java)

Code (Python)

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated