chore(CPlusPlus): add reverse in groups of K (#1106)
parent
73a79fd221
commit
bb641ee600
|
@ -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<bits/stdc++.h>
|
||||||
|
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<<endl;
|
||||||
|
printList(head);
|
||||||
|
}
|
|
@ -81,6 +81,7 @@
|
||||||
- [Segregate Even Odd Nodes of linked list](Linked-Lists/segregate-even-odd-nodes-of-linked-list.cpp)
|
- [Segregate Even Odd Nodes of linked list](Linked-Lists/segregate-even-odd-nodes-of-linked-list.cpp)
|
||||||
- [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp)
|
- [Remove Duplicate in Sorted linked list](Linked-Lists/remove-duplicates-in-sorted-linked-list.cpp)
|
||||||
- [Reverse the linked list using stack](Linked-Lists/reverse-the-list-using-stack.cpp)
|
- [Reverse the linked list using stack](Linked-Lists/reverse-the-list-using-stack.cpp)
|
||||||
|
- [Reverse the linked list in groups of K](Linked-Lists/reverse-the-list-in-groups-of-k.cpp)
|
||||||
## Searching
|
## Searching
|
||||||
|
|
||||||
- [Linear Search](Searching/linear-search.cpp)
|
- [Linear Search](Searching/linear-search.cpp)
|
||||||
|
|
Loading…
Reference in New Issue