Hackerrank | 30 days of code | Day 24-Solution in c++| More-Linked-Lists-hackerrank-Solution-in-c++


Hackerrank | 30 days of code | Day 24-Solution in c++| More-Linked-Lists-hackerrank-Solution-in-c++

\
Hackerrank Solution 30-days-of-code

https://www.hackerrank.com/challenges/30-2D-arrays/problem
Day 24 More Linked Lists Solution in Cpp
#include <iostream> #include <vector> #include <cstddef> #include <cmath> #include <cstdio> #include <algorithm> using namespace std; class Node { public: int data; Node *next; Node(int d){ data=d; next=NULL; } }; class Solution{ public: Node* removeDuplicates(Node *head){ if (!head) return head; Node *node = head; while (node->next) { if (node->data == node->next->data) node->next = node->next->next; else node = node->next; } return head; } Node* insert(Node *head,int data) { Node* p=new Node(data); if(head==NULL){ head=p; } else if(head->next==NULL){ head->next=p; } else{ Node *start=head; while(start->next!=NULL){ start=start->next; } start->next=p; } return head; } void display(Node *head) { Node *start=head; while(start) { cout<<start->data<<" "; start=start->next; } } }; int main() { Node* head=NULL; Solution mylist; int T,data; cin>>T; while(T-->0){ cin>>data; head=mylist.insert(head,data); } head=mylist.removeDuplicates(head); mylist.display(head); }

Post a Comment

0 Comments