From 4a49218b4c656e31db52284d338fa373da27c5c1 Mon Sep 17 00:00:00 2001 From: Animesh sharma <101627366+Animeshsharma4@users.noreply.github.com> Date: Mon, 10 Oct 2022 10:16:24 +0530 Subject: [PATCH] added hackerrank solutions --- hacker rank solutions/Delete a node.cpp | 18 ++++ hacker rank solutions/Left Rotation.cpp | 18 ++++ hacker rank solutions/arrays-DS.cpp | 100 ++++++++++++++++++ ...t a specific position in a linked list.cpp | 30 ++++++ ...insert the node at head of linked list.cpp | 6 ++ ...rt the node at the tail of linked list.cpp | 15 +++ hacker rank solutions/print in reverse.cpp | 18 ++++ .../print the element of linked list.cpp | 10 ++ .../reverse a linked list.cpp | 20 ++++ 9 files changed, 235 insertions(+) create mode 100644 hacker rank solutions/Delete a node.cpp create mode 100644 hacker rank solutions/Left Rotation.cpp create mode 100644 hacker rank solutions/arrays-DS.cpp create mode 100644 hacker rank solutions/insert a node at a specific position in a linked list.cpp create mode 100644 hacker rank solutions/insert the node at head of linked list.cpp create mode 100644 hacker rank solutions/insert the node at the tail of linked list.cpp create mode 100644 hacker rank solutions/print in reverse.cpp create mode 100644 hacker rank solutions/print the element of linked list.cpp create mode 100644 hacker rank solutions/reverse a linked list.cpp diff --git a/hacker rank solutions/Delete a node.cpp b/hacker rank solutions/Delete a node.cpp new file mode 100644 index 00000000..eb5eeae4 --- /dev/null +++ b/hacker rank solutions/Delete a node.cpp @@ -0,0 +1,18 @@ + + + +SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* llist, int position) { +int cnt=0; +SinglyLinkedListNode* temp=llist; + +while(cntnext; + cnt++; +} +if(position==0) { + llist=temp->next; +} +temp->next=temp->next->next; + +return llist; +} diff --git a/hacker rank solutions/Left Rotation.cpp b/hacker rank solutions/Left Rotation.cpp new file mode 100644 index 00000000..adc31cec --- /dev/null +++ b/hacker rank solutions/Left Rotation.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +#include +int main(){ + int n1,n2; + cin>>n1>>n2; + vector vect(n1); + for(int i=0;i>vect[i]; + } + n2=n2%n1; + for(int j=n2;j + +using namespace std; + +string ltrim(const string &); +string rtrim(const string &); +vector split(const string &); + +/* + * Complete the 'reverseArray' function below. + * + * The function is expected to return an INTEGER_ARRAY. + * The function accepts INTEGER_ARRAY a as parameter. + */ + +vector reverseArray(vector a) { +vectorresult; +for(int i=a.size()-1;i>=0;i--){ + result.push_back(a[i]); +} +return result; +} + +int main() +{ + ofstream fout(getenv("OUTPUT_PATH")); + + string arr_count_temp; + getline(cin, arr_count_temp); + + int arr_count = stoi(ltrim(rtrim(arr_count_temp))); + + string arr_temp_temp; + getline(cin, arr_temp_temp); + + vector arr_temp = split(rtrim(arr_temp_temp)); + + vector arr(arr_count); + + for (int i = 0; i < arr_count; i++) { + int arr_item = stoi(arr_temp[i]); + + arr[i] = arr_item; + } + + vector res = reverseArray(arr); + + for (size_t i = 0; i < res.size(); i++) { + fout << res[i]; + + if (i != res.size() - 1) { + fout << " "; + } + } + + fout << "\n"; + + fout.close(); + + return 0; +} + +string ltrim(const string &str) { + string s(str); + + s.erase( + s.begin(), + find_if(s.begin(), s.end(), not1(ptr_fun(isspace))) + ); + + return s; +} + +string rtrim(const string &str) { + string s(str); + + s.erase( + find_if(s.rbegin(), s.rend(), not1(ptr_fun(isspace))).base(), + s.end() + ); + + return s; +} + +vector split(const string &str) { + vector tokens; + + string::size_type start = 0; + string::size_type end = 0; + + while ((end = str.find(" ", start)) != string::npos) { + tokens.push_back(str.substr(start, end - start)); + + start = end + 1; + } + + tokens.push_back(str.substr(start)); + + return tokens; +} diff --git a/hacker rank solutions/insert a node at a specific position in a linked list.cpp b/hacker rank solutions/insert a node at a specific position in a linked list.cpp new file mode 100644 index 00000000..f63e06f9 --- /dev/null +++ b/hacker rank solutions/insert a node at a specific position in a linked list.cpp @@ -0,0 +1,30 @@ + +/* + * For your reference: + * + * SinglyLinkedListNode { + * int data; + * SinglyLinkedListNode* next; + * }; + * + */ + +SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) { + SinglyLinkedListNode*temp=llist; + int cnt=0; + SinglyLinkedListNode*node1=new SinglyLinkedListNode(data); + if(position==1){ + node1->next=llist; + llist=node1; + } + else{ + while(cntnext; + } + node1->next=temp->next; + temp->next=node1; + } + + return llist; +} diff --git a/hacker rank solutions/insert the node at head of linked list.cpp b/hacker rank solutions/insert the node at head of linked list.cpp new file mode 100644 index 00000000..0ebd98ea --- /dev/null +++ b/hacker rank solutions/insert the node at head of linked list.cpp @@ -0,0 +1,6 @@ +SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) { + SinglyLinkedListNode* temp=new SinglyLinkedListNode(data); + temp->next=llist; + llist=temp; + return llist; +} diff --git a/hacker rank solutions/insert the node at the tail of linked list.cpp b/hacker rank solutions/insert the node at the tail of linked list.cpp new file mode 100644 index 00000000..7680f752 --- /dev/null +++ b/hacker rank solutions/insert the node at the tail of linked list.cpp @@ -0,0 +1,15 @@ +SinglyLinkedListNode*insertNodeAtTail( SinglyLinkedListNode* head,int data){ + SinglyLinkedListNode*temp=head; + SinglyLinkedListNode*node1=new SinglyLinkedListNode(data); + if(head==0){ + head=node1; + } + else{ + while(temp->next!=NULL){ + temp=temp->next; + } + temp->next=node1; + + } + return head; + } diff --git a/hacker rank solutions/print in reverse.cpp b/hacker rank solutions/print in reverse.cpp new file mode 100644 index 00000000..840f403a --- /dev/null +++ b/hacker rank solutions/print in reverse.cpp @@ -0,0 +1,18 @@ +SinglyLinkedListNode* reversePrint(SinglyLinkedListNode* llist) { + if(llist==NULL || llist->next==NULL){ + return llist; + } +SinglyLinkedListNode* curr=llist; +SinglyLinkedListNode* prev=NULL; +SinglyLinkedListNode* forward=NULL; + +while(curr!=NULL){ + forward=curr->next; + curr->next=prev; + prev=curr; + curr=forward; + + +} +return prev; +} diff --git a/hacker rank solutions/print the element of linked list.cpp b/hacker rank solutions/print the element of linked list.cpp new file mode 100644 index 00000000..cd34986a --- /dev/null +++ b/hacker rank solutions/print the element of linked list.cpp @@ -0,0 +1,10 @@ +void printLinkedList(SinglyLinkedListNode* head) { + SinglyLinkedListNode*temp=head; + while(temp!=NULL){ + cout<data<next; + } + + + +} diff --git a/hacker rank solutions/reverse a linked list.cpp b/hacker rank solutions/reverse a linked list.cpp new file mode 100644 index 00000000..6265d337 --- /dev/null +++ b/hacker rank solutions/reverse a linked list.cpp @@ -0,0 +1,20 @@ + + +SinglyLinkedListNode* reverse(SinglyLinkedListNode* llist) { + if(llist==NULL || llist->next==NULL){ + return llist; + } +SinglyLinkedListNode* curr=llist; +SinglyLinkedListNode* prev=NULL; +SinglyLinkedListNode* forward=NULL; + +while(curr!=NULL){ + forward=curr->next; + curr->next=prev; + prev=curr; + curr=forward; + + +} +return prev; +}