
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Remove Duplicates from Sorted List in C++
Suppose we have a sorted linked list; we have to delete all duplicates such that each element appears only once.
So, if the input is like [1,1,2,3,3,3,4,5,5], then the output will be [1,2,3,4,5]
To solve this, we will follow these steps −
dummy := make a new node with value -inf
next of dummy := head
curr = dummy
while curr is non-zero, do −
next = next of curr
while (next is not null and val of next is same as val of curr), do −
next := next of next
next of curr := next
curr := next
return next of dummy
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class ListNode{ public: int val; ListNode *next; ListNode(int data){ val = data; next = NULL; } }; ListNode *make_list(vector<int> v){ ListNode *head = new ListNode(v[0]); for(int i = 1; i<v.size(); i++){ ListNode *ptr = head; while(ptr->next != NULL){ ptr = ptr->next; } ptr->next = new ListNode(v[i]); } return head; } void print_list(ListNode *head){ ListNode *ptr = head; cout << "["; while(ptr){ cout << ptr->val << ", "; ptr = ptr->next; } cout << "]" << endl; } class Solution { public: ListNode* deleteDuplicates(ListNode* head) { ListNode*dummy = new ListNode(INT_MIN); dummy->next = head; ListNode * curr = dummy; while(curr){ ListNode * next = curr->next; while(next && next->val==curr->val) next = next->next; curr->next = next; curr=next; } return dummy->next; } }; main(){ Solution ob; vector<int> v = {1,1,2,3,3,3,4,5,5}; ListNode *head = make_list(v); print_list(ob.deleteDuplicates(head)); }
Input
{1,1,2,3,3,3,4,5,5}
Output
[1, 2, 3, 4, 5, ]
- Related Articles
- Remove Duplicates from Sorted List II in C++
- Remove Duplicates from a Sorted Linked List Using Recursion
- Remove Duplicates from Sorted Array II in C++
- How to remove duplicates from a sorted linked list in android?
- Remove Duplicates from Sorted Array in Python
- Remove duplicates from a List in C#
- Python - Ways to remove duplicates from list
- How to remove duplicates from the sorted array and return the length using C#?
- Javascript Program For Removing Duplicates From A Sorted Linked List
- How do you remove duplicates from a list in Python?
- Python program to remove Duplicates elements from a List?
- Java program to remove duplicates elements from a List
- Java Program to Remove Duplicates from an Array List
- How to remove duplicates from the sorted array and return the non-duplicated array using C#?
- Golang program to remove duplicates from a sorted array using two-pointer approach

Advertisements