Skip to content

Commit ffc508d

Browse files
committed
Remove Duplicates from a Sorted List
1 parent 9b08e63 commit ffc508d

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// we will be writing the code for Removing Duplicates from Sorted List
2+
#include<iostream>
3+
using namespace std;
4+
struct ListNode{
5+
int data;
6+
ListNode * next;
7+
ListNode(int val): data(val), next(nullptr){}
8+
};
9+
ListNode * deleteDuplicates(ListNode * head){
10+
if(head == NULL){
11+
return NULL;
12+
}
13+
ListNode * current = head;
14+
while(current != NULL){
15+
if(current->next != NULL && current->data == current->next->data){
16+
ListNode * nextNode = current->next->next;
17+
ListNode * nodetodelete = current->next;
18+
delete nodetodelete;
19+
current->next = nextNode;
20+
}
21+
else{
22+
current = current->next;
23+
}
24+
}
25+
return head;
26+
}
27+
void printlist(ListNode * head){
28+
ListNode * temp = head;
29+
while(temp != NULL){
30+
cout << temp->data << " ";
31+
temp = temp->next;
32+
}
33+
cout << endl;
34+
}
35+
int main(){
36+
ListNode * head = new ListNode(1);
37+
head->next = new ListNode(2);
38+
head->next->next = new ListNode(2);
39+
head->next->next->next = new ListNode(3);
40+
head->next->next->next->next = new ListNode(3);
41+
head->next->next->next->next->next = new ListNode(4);
42+
cout << "Original Linked List: ";
43+
printlist(head);
44+
head = deleteDuplicates(head);
45+
cout << "Linked List After Removing Duplicates: ";
46+
printlist(head);
47+
return 0;
48+
}

Leetcode_Practice_Questions/LC_869.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ using namespace std;
33
#include<algorithm>
44
#include<string>
55
#include<cmath>
6-
76
string getSortedStr(int n) {
87
string s = to_string(n);
98
sort(begin(s), end(s));
109
return s;
1110
}
12-
1311
bool reorderedPowerOf2(int n) {
1412
string s = getSortedStr(n);
1513
for(int p = 0; p <= 29; p++) {

0 commit comments

Comments
 (0)