From bb641ee600cec57d727ccaf9373654349c9d318c Mon Sep 17 00:00:00 2001 From: RK-Shandilya <89962501+RK-Shandilya@users.noreply.github.com> Date: Thu, 22 Dec 2022 18:58:01 +0530 Subject: [PATCH] chore(CPlusPlus): add reverse in groups of K (#1106) --- .../Linked-Lists/reverseInGroupsOfK.cpp | 79 +++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 80 insertions(+) create mode 100644 algorithms/CPlusPlus/Linked-Lists/reverseInGroupsOfK.cpp diff --git a/algorithms/CPlusPlus/Linked-Lists/reverseInGroupsOfK.cpp b/algorithms/CPlusPlus/Linked-Lists/reverseInGroupsOfK.cpp new file mode 100644 index 00000000..a0de9bb8 --- /dev/null +++ b/algorithms/CPlusPlus/Linked-Lists/reverseInGroupsOfK.cpp @@ -0,0 +1,79 @@ +// Description := Reverse a linkedlist in groups of size K . + +// Time and space complexity :- +// Time Complexity = O(N) and Space Complexity = O(N) + +// Example :- +// Input: 1->2->3->4->5->6->7->8->NULL, K = 3 +// Output: 3->2->1->6->5->4->8->7->NULL + +#include +using namespace std; + +class Node{ + public: + int data; + Node* next; +}; + +void push(Node* &head_ref, int new_data) +{ + Node* new_node = new Node(); + + new_node->data = new_data; + + new_node->next = head_ref; + + head_ref = new_node; +} + +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data << " "; + node = node->next; + } +} + +Node* kReverse(Node* &head, int k) { + + // base case + if(head == NULL) { + return NULL; + } + + Node* next = NULL; + Node* curr = head; + Node* prev = NULL; + int count= 0; + + while( curr != NULL && count < k ) { + next = curr -> next; + curr -> next = prev; + prev = curr; + curr = next; + count++; + } + + if(next != NULL) { + head -> next = kReverse(next,k); + } + return prev; +} + +int main(){ + Node* head=NULL; + Node* ans =head; + push(head,1); + push(head,2); + push(head,3); + push(head,4); + push(head,5); + push(head,6); + push(head,7); + push(head,8); + printList(head); + head = kReverse(head,3); + cout<