Category: Reverse

Print array after it is right rotated K times | Set 2

Given an array arr[] of size N and a value K, the task is to print the array rotated by K times to the right.Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your […]

Continue Reading

Minimum number of adjacent swaps to reverse a String

Given a string s. The task is to minimize the number of adjacent swaps required to reverse the string. Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to […]

Continue Reading

Reverse a singly Linked List in groups of given size | Set 4 (Space efficient approach)

  class LinkedList {          Node head;          class Node {        int data;        Node next;        Node(int d)        {            data = d;            next = null;        }    }      Node reverse(Node node, int k)    {        if (k == 1)            return node;                  Node result = null;                  Node head = new Node(0);        head.next = node;        Node next = node.next;          int count = 0;        while (next != null) {            count++;                                                  Node tmp = next.next;            next.next = node;            node = next;            next = tmp;              if (count == k – 1) {                count […]

Continue Reading

Modify a Linked List to contain last occurrences of every duplicate element

#include using namespace std;  class Node {public:    int data;    Node* next;      Node(int x)    {        this->data = x;        this->next = NULL;    }};  Node* reverseList(Node* head){    Node *prev = NULL, *nextNode = NULL;    while (head != NULL) {                          nextNode = head->next;                          head->next = prev;          prev = head;          head = nextNode;    }    return prev;}  Node* Remove_Dup_Keep_Last_Occurence(    Node* head){        Node* dummy = new Node(-1);    dummy->next = head;          dummy->next = reverseList(dummy->next);          unordered_set visited;      Node *currNode = dummy, *nextNode;          while (currNode != NULL           && currNode->next != NULL) […]

Continue Reading

Reversing a Stack with the help of another empty Stack

Reversing a Stack with the help of another empty StackGiven a Stack consisting of N elements, the task is to reverse the Stack using an extra stack.Examples:Input: stack = {1, 2, 3, 4, 5}Output:12345Explanation:Input Stack:54321Reversed Stack:12345Input: stack = {1, 3, 5, 4, 2}Output:13542Approach: Follow the steps below to solve the problem:Below is the implementation of […]

Continue Reading

Sort a string lexicographically by reversing a substring

  #include using namespace std;  bool adjust(string& S, int& i,            int& start, int& end){        int N = S.length();              start = i – 1;              while (i < N && S[i] < S[i - 1]) {                  i++;    }              end = i - 1;          if (start = N)        return true;          if (start >= 1 && i = S[start – 1]                && S[start] = 1) {                  return S[end] >= S[start – 1];    }          if (i […]

Continue Reading

XOR linked list: Reverse last K nodes of a Linked List

   #include #include #include    struct Node {                  int data;                  struct Node* nxp; };    struct Node* XOR(struct Node* a,                  struct Node* b) {     return (struct Node*)((uintptr_t)(a)                           ^ (uintptr_t)(b)); }    struct Node* insert(struct Node** head,                     int value) {          if (*head == NULL) {                     struct Node* […]

Continue Reading