Banana-Leopard/TASTY huggingface dataset:
#include<iostream>
using namespace std;
 
class Node
{
    public:
        char data;
        Node* next;
        Node* pre;
    Node(int data)
    {
        this->data=data;
        pre=NULL;
        next=NULL;
    }
};
 
void insertAtHead(Node* &head, int data)
{
    Node* n = new Node(data);
    if(head==NULL)
    {
        head=n;
        return;
    }
    n->next=head;
    head->pre=n;
    head=n;
    return;
}
void insertAtTail(Node* &head, int data)
{
    if(head==NULL)
    {
        insertAtHead(head,data);
        return;
    }
    Node* temp=head;
    while(temp->next!=NULL)
    {
        temp=temp->next;
    }
    Node* n=new Node(data);
    temp->next=n;
    n->pre=temp;
    return;
}
void display(Node* head)
{
    while(head!=NULL)
    {
        cout << head->data << "-->";
        head=head->next;
    }
    cout << "NULL\n";
}
 
void rotateByN(Node *&head, int pos)
{
    if (pos == 0)
        return;
 
    Node *curr = head;
 
    while (pos)
    {
        curr = curr->next;
        pos--;
    }
 
    Node *tail = curr->pre;
    Node *NewHead = curr;
    tail->next = NULL;
    curr->pre = NULL;
 
    while (curr->next != NULL)
    {
        curr = curr->next;
    }
     
    curr->next = head;
    head->pre = curr;
    head = NewHead;
}
  
int main()
{
    Node* head=NULL;
    insertAtTail(head,'a');
    insertAtTail(head,'b');
    insertAtTail(head,'c');
    insertAtTail(head,'d');
    insertAtTail(head,'e');
 
    int n=2;
    cout << "\nBefore Rotation : \n";
    display(head);
    rotateByN(head,n);
    cout << "\nAfter Rotation : \n";
    display(head);
    cout << "\n\n";
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ implementation to reverse a doubly linked list
// in groups of given size without recursion
// Iterative Method
 
#include <iostream>
using namespace std;
 
// Represents a node of doubly linked list
struct Node {
    int data;
    Node *next, *prev;
};
 
// function to get a new node
Node* getNode(int data)
{
    // allocating node
    Node* new_node = new Node();
    new_node->data = data;
    new_node->next = new_node->prev = NULL;
 
    return new_node;
}
 
// function to insert a node at the beginning
// of the Doubly Linked List
Node* push(Node* head, Node* new_node)
{
    // since we are adding at the beginning,
    // prev is always NULL
    new_node->prev = NULL;
 
    // link the old list off the new node
    new_node->next = head;
    // change prev of head node to new node
    if (head != NULL)
        head->prev = new_node;
 
    // move the head to point to the new node
    head = new_node;
    return head;
}
 
// function to reverse a doubly linked list
// in groups of given size
Node* revListInGroupOfGivenSize(Node* head, int k)
{
    if (!head)
        return head;
 
    Node* st = head;
    Node* globprev = NULL;
    Node* ans = NULL;
    while (st) {
        int count = 1; // to count k nodes
        Node* curr = st;
        Node* prev = NULL;
        Node* next = NULL;
        while (curr && count <= k) { // reversing k nodes
            next = curr->next;
            curr->prev = next;
            curr->next = prev;
            prev = curr;
            curr = next;
            count++;
        }
 
        if (!ans) {
            ans = prev; // to store ans i.e the new head
            ans->prev = NULL;
        }
 
        if (!globprev)
            globprev = st; // assigning the last node of the
                           // reversed k nodes
        else {
            globprev->next = prev;
            prev->prev
                = globprev; // connecting last node of last
                            // k group to the first node of
                            // present k group
            globprev = st;
        }
 
        st = curr; // advancing the pointer for the next k
                   // group
    }
    return ans;
}
 
// Function to print nodes in a
// given doubly linked list
void printList(Node* head)
{
    while (head) {
        cout << head->data << " ";
        head = head->next;
    }
}
 
// Driver code
int main()
{
    // Start with the empty list
    Node* head = NULL;
 
    // Create doubly linked: 10<->8<->4<->2
    head = push(head, getNode(2));
    head = push(head, getNode(4));
    head = push(head, getNode(8));
    head = push(head, getNode(10));
 
    int k = 2;
 
    cout << "Original list: ";
    printList(head);
 
    // Reverse doubly linked list in groups of
    // size 'k'
    head = revListInGroupOfGivenSize(head, k);
 
    cout << "\nModified list: ";
    printList(head);
    return 0;
}
 
// This code is contributed by Tapesh (tapeshdua420)

Time complexity: linear
Space complexity: constant
// C++ program to implement Stack
// using linked list so that reverse
// can be done with O(1) extra space.
#include<bits/stdc++.h>
using namespace std;
 
class StackNode {
    public:
    int data;
    StackNode *next;
     
    StackNode(int data)
    {
        this->data = data;
        this->next = NULL;
    }
};
 
class Stack {
     
    StackNode *top;
     
    public:
     
    // Push and pop operations
    void push(int data)
    {
        if (top == NULL) {
            top = new StackNode(data);
            return;
        }
        StackNode *s = new StackNode(data);
        s->next = top;
        top = s;
    }
     
    StackNode* pop()
    {
        StackNode *s = top;
        top = top->next;
        return s;
    }
 
    // prints contents of stack
    void display()
    {
        StackNode *s = top;
        while (s != NULL) {
            cout << s->data << " ";
            s = s->next;
        }
        cout << endl;
    }
 
    // Reverses the stack using simple
    // linked list reversal logic.
    void reverse()
    {
        StackNode *prev, *cur, *succ;
        cur = prev = top;
        cur = cur->next;
        prev->next = NULL;
        while (cur != NULL) {
 
            succ = cur->next;
            cur->next = prev;
            prev = cur;
            cur = succ;
        }
        top = prev;
    }
};
 
// driver code
int main()
{
    Stack *s = new Stack();
    s->push(1);
    s->push(2);
    s->push(3);
    s->push(4);
    cout << "Original Stack" << endl;;
    s->display();
    cout << endl;
     
    // reverse
    s->reverse();
 
    cout << "Reversed Stack" << endl;
    s->display();
     
    return 0;
}
// This code is contributed by Chhavi.

Time complexity: linear
Space complexity: constant
// C++ program to implement unrolled linked list
// and traversing it.
#include <bits/stdc++.h>
using namespace std;
#define maxElements 4
 
// Unrolled Linked List Node
class Node
{
    public:
    int numElements;
    int array[maxElements];
    Node *next;
};
 
/* Function to traverse an unrolled linked list
and print all the elements*/
void printUnrolledList(Node *n)
{
    while (n != NULL)
    {
        // Print elements in current node
        for (int i=0; i<n->numElements; i++)
            cout<<n->array[i]<<" ";
 
        // Move to next node
        n = n->next;
    }
}
 
// Program to create an unrolled linked list
// with 3 Nodes
int main()
{
    Node* head = NULL;
    Node* second = NULL;
    Node* third = NULL;
 
    // allocate 3 Nodes
    head = new Node();
    second = new Node();
    third = new Node();
 
    // Let us put some values in second node (Number
    // of values must be less than or equal to
    // maxElement)
    head->numElements = 3;
    head->array[0] = 1;
    head->array[1] = 2;
    head->array[2] = 3;
 
    // Link first Node with the second Node
    head->next = second;
 
    // Let us put some values in second node (Number
    // of values must be less than or equal to
    // maxElement)
    second->numElements = 3;
    second->array[0] = 4;
    second->array[1] = 5;
    second->array[2] = 6;
 
    // Link second Node with the third Node
    second->next = third;
 
    // Let us put some values in third node (Number
    // of values must be less than or equal to
    // maxElement)
    third->numElements = 3;
    third->array[0] = 7;
    third->array[1] = 8;
    third->array[2] = 9;
    third->next = NULL;
 
    printUnrolledList(head);
 
    return 0;
}
 
// This is code is contributed by rathbhupendra

Time complexity: linear
Space complexity: linear
// C++ program to find fractional node in a linked list
#include <bits/stdc++.h>
 
/* Linked list node */
struct Node {
    int data;
    Node* next;
};
 
/* Function to create a new node with given data */
Node* newNode(int data)
{
    Node* new_node = new Node;
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}
 
/* Function to find fractional node in the linked list */
Node* fractionalNodes(Node* head, int k)
{
    // Corner cases
    if (k <= 0 || head == NULL)
        return NULL;
 
    Node* fractionalNode = NULL;
     
    // Traverse the given list
    int i = 0;
    for (Node* temp = head; temp != NULL; temp = temp->next) {
 
        // For every k nodes, we move fractionalNode one
        // step ahead.
        if (i % k == 0) {
 
            // First time we see a multiple of k
            if (fractionalNode == NULL)
                fractionalNode = head;
 
            else
                fractionalNode = fractionalNode->next;
        }
        i++;
    }
    return fractionalNode;
}
 
// A utility function to print a linked list
void printList(Node* node)
{
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
    printf("\n");
}
 
/* Driver program to test above function */
int main(void)
{
    Node* head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(3);
    head->next->next->next = newNode(4);
    head->next->next->next->next = newNode(5);
    int k = 2;
 
    printf("List is ");
    printList(head);
 
    Node* answer = fractionalNodes(head, k);
    printf("\nFractional node is ");
    printf("%d\n", answer->data);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find modular node in a linked list
#include <bits/stdc++.h>
 
/* Linked list node */
struct Node {
    int data;
    Node* next;
};
 
/* Function to create a new node with given data */
Node* newNode(int data)
{
    Node* new_node = new Node;
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}
 
/* Function to find modular node in the linked list */
Node* modularNode(Node* head, int k)
{
    // Corner cases
    if (k <= 0 || head == NULL)
        return NULL;  
 
    // Traverse the given list
    int i = 1;
    Node* modularNode = NULL;
    for (Node* temp = head; temp != NULL; temp = temp->next) {
        if (i % k == 0)
            modularNode = temp;
         
        i++;
    }
    return modularNode;
}
 
/* Driver program to test above function */
int main(void)
{
    Node* head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(3);
    head->next->next->next = newNode(4);
    head->next->next->next->next = newNode(5);
    int k = 2;
    Node* answer = modularNode(head, k);
    printf("\nModular node is ");
    if (answer != NULL)
        printf("%d\n", answer->data);
    else
        printf("null\n");
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ Program to find smallest and largest
// elements in singly linked list.
#include <bits/stdc++.h>
 
using namespace std;
/* Linked list node */
struct Node {
    int data;
    struct Node* next;
};
 
// Function that returns the largest element
// from the linked list.
int largestElement(struct Node* head)
{
    // Declare a max variable and initialize
    // it with INT_MIN value.
    // INT_MIN is integer type and its value
    // is -32767 or less.
    int max = INT_MIN;
 
    // Check loop while head not equal to NULL
    while (head != NULL) {
 
        // If max is less than head->data then
        // assign value of head->data to max
        // otherwise node point to next node.
        if (max < head->data)
            max = head->data;
        head = head->next;
    }
    return max;
}
 
// Function that returns smallest element
// from the linked list.
int smallestElement(struct Node* head)
{
    // Declare a min variable and initialize
    // it with INT_MAX value.
    // INT_MAX is integer type and its value
    // is 32767 or greater.
    int min = INT_MAX;
 
    // Check loop while head not equal to NULL
    while (head != NULL) {
 
        // If min is greater than head->data then
        // assign value of head->data to min
        // otherwise node point to next node.
        if (min > head->data)
            min = head->data;
 
        head = head->next;
    }
    return min;
}
 
// Function that push the element in linked list.
void push(struct Node** head, int data)
{
    // Allocate dynamic memory for newNode.
    struct Node* newNode =
         (struct Node*)malloc(sizeof(struct Node));
 
    // Assign the data into newNode.
    newNode->data = data;
 
    // newNode->next assign the address of
    // head node.
    newNode->next = (*head);
 
    // newNode become the headNode.
    (*head) = newNode;
}
 
// Display linked list.
void printList(struct Node* head)
{
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    cout << "NULL" << endl;
}
 
// Driver program to test the functions
int main()
{
    // Start with empty list
    struct Node* head = NULL;
 
    // Using push() function to construct
    // singly linked list
    // 17->22->13->14->15
    push(&head, 15);
    push(&head, 14);
    push(&head, 13);
    push(&head, 22);
    push(&head, 17);
    cout << "Linked list is : " << endl;
 
    // Call printList() function to display
    // the linked list.
    printList(head);
    cout << "Maximum element in linked list:";
 
    // Call largestElement() function to get largest
    // element in linked list.
    cout << largestElement(head) << endl;
    cout << "Minimum element in linked list:";
 
    // Call smallestElement() function to get smallest
    // element in linked list.
    cout << smallestElement(head) << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: constant
/* C++ program to arrange consonants and
vowels nodes in a linked list */
#include <bits/stdc++.h>
using namespace std;
 
/* A linked list node */
struct Node {
    char data;
    struct Node* next;
    Node(int x)
    {
        data = x;
        next = NULL;
    }
};
 
/* Function to add new node to the List */
void append(struct Node** headRef, char data)
{
    struct Node* new_node = new Node(data);
    struct Node* last = *headRef;
    if (*headRef == NULL) {
        *headRef = new_node;
        return;
    }
    while (last->next != NULL)
        last = last->next;
    last->next = new_node;
    return;
}
 
// utility function to print linked list
void printlist(Node* head)
{
    if (!head) {
        cout << "Empty List\n";
        return;
    }
    while (head != NULL) {
        cout << head->data << " ";
        if (head->next)
            cout << "-> ";
        head = head->next;
    }
    cout << endl;
}
 
/* function to arrange consonants and
vowels nodes */
 
struct Node* arrange(Node* head)
{
    Node *vowel = NULL, *consonant = NULL, *start = NULL,
         *end = NULL;
    while (head != NULL) {
        char x = head->data;
        // Checking the current node data is vowel or
        // not
        if (x == 'a' || x == 'e' || x == 'i' || x == 'o'
            || x == 'u') {
            if (!vowel) {
                vowel = new Node(x);
                start = vowel;
            }
            else {
                vowel->next = new Node(x);
                vowel = vowel->next;
            }
        }
        else {
            if (!consonant) {
                consonant = new Node(x);
                end = consonant;
            }
            else {
                consonant->next = new Node(x);
                consonant = consonant->next;
            }
        }
        head = head->next;
    }
    // In case when there is no vowel in the incoming LL
    // then we have to return the head of the consonant LL
    if (start == NULL)
        return end;
    // Connecting the vowel and consonant LL
    vowel->next = end;
    return start;
}
 
// Driver code
int main()
{
    struct Node* head = NULL;
    append(&head, 'a');
    append(&head, 'b');
    append(&head, 'c');
    append(&head, 'e');
    append(&head, 'd');
    append(&head, 'o');
    append(&head, 'x');
    append(&head, 'i');
 
    printf("Linked list before :\n");
    printlist(head);
 
    head = arrange(head);
 
    printf("Linked list after :\n");
    printlist(head);
 
    return 0;
}
 
// This code is contributed by Aditya Kumar

Time complexity: linear
Space complexity: linear
// C++ program to partition a linked list around a
// given value.
#include<bits/stdc++.h>
using namespace std;
 
/* Link list Node */
struct Node
{
    int data;
    struct Node* next;
};
 
// A utility function to create a new node
Node *newNode(int data)
{
    struct Node* new_node = new Node;
    new_node->data  = data;
    new_node->next = NULL;
    return new_node;
}
 
// Function to make a new list(using the existing
// nodes) and return head of new list.
struct Node *partition(struct Node *head, int x)
{
    /* Let us initialize start and tail nodes of
    new list */
    struct Node *tail = head;
 
    // Now iterate original list and connect nodes
    Node *curr = head;
    while (curr != NULL)
    {
        struct Node *next = curr->next;
        if (curr->data < x)
        {
            /* Insert node at head. */
            curr->next = head;
            head = curr;
        }
 
        else // Append to the list of greater values
        {
            /* Insert node at tail. */
            tail->next = curr;
            tail = curr;
        }
        curr = next;
    }
    tail->next = NULL;
 
    // The head has changed, so we need
    // to return it to the user.
    return head;
}
 
/* Function to print linked list */
void printList(struct Node *head)
{
    struct Node *temp = head;
    while (temp != NULL)
    {
        printf("%d  ", temp->data);
        temp = temp->next;
    }
}
 
// Driver program to run the case
int main()
{
    /* Start with the empty list */
    struct Node* head = newNode(3);
    head->next = newNode(5);
    head->next->next = newNode(8);
    head->next->next->next = newNode(2);
    head->next->next->next->next = newNode(10);
    head->next->next->next->next->next = newNode(2);
    head->next->next->next->next->next->next = newNode(1);
 
    int x = 5;
    head = partition(head, x);
    printList(head);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ implementation to modify the
// contents of the linked list
#include <bits/stdc++.h>
using namespace std;
 
// Linked list node
struct Node
{
    int data;
    struct Node* next;
};
 
// function prototype for printing the list
void printList(struct Node*);
 
// Function to insert a node at the
// beginning of the linked list
void push(struct Node **head_ref, int new_data)
{
 
// allocate node
struct Node* new_node =
            (struct Node*) malloc(sizeof(struct Node));
 
// put in the data
new_node->data = new_data;
 
// link the old list at the end of the new node
new_node->next = *head_ref;
 
// move the head to point to the new node
*head_ref = new_node;
}
 
// function to print the linked list
void printList(struct Node *head)
{
    if (!head)
        return;
     
    while (head->next != NULL)
    {
        cout << head->data << " -> ";
        head = head->next;
    }
    cout << head->data << endl;
}
 
// Function to middle node of list.
Node* find_mid(Node *head)
{
    Node *temp = head, *slow = head, *fast = head ;
     
    while(fast && fast->next)
    {
         
    // Advance 'fast' two nodes, and
    // advance 'slow' one node
    slow = slow->next ;
    fast = fast->next->next ;
    }
     
    // If number of nodes are odd then update slow
    // by slow->next;
    if(fast)
    slow = slow->next ;
 
return slow ;
}
 
// function to modify the contents of the linked list.
void modifyTheList(struct Node *head, struct Node *slow)
{
// Create Stack.
stack <int> s;
Node *temp = head ;
 
while(slow)
{
    s.push( slow->data ) ;
    slow = slow->next ;
}
 
// Traverse the list by using temp until stack is empty.
while( !s.empty() )
{
    temp->data = temp->data - s.top() ;
    temp = temp->next ;
    s.pop() ;
}
 
}
 
// Driver program to test above
int main()
{
    struct Node *head = NULL, *mid ;
     
    // creating the linked list
    push(&head, 10);
    push(&head, 7);
    push(&head, 12);
    push(&head, 8);
    push(&head, 9);
    push(&head, 2);
     
    // Call Function to Find the starting point of second half of list.
    mid = find_mid(head) ;
     
    // Call function to modify the contents of the linked list.
    modifyTheList( head, mid);
     
     
    // print the modified linked list
    cout << "Modified List:" << endl;
    printList(head);
    return 0;
}
 
// This is contributed by Mr. Gera

Time complexity: linear
Space complexity: linear
// C++ program to shift k elements in a matrix.
#include <bits/stdc++.h>
using namespace std;
#define N 4
 
// Function to shift first k elements of
// each row of matrix.
void shiftMatrixByK(int mat[N][N], int k)
{
    if (k > N) {
        cout << "shifting is not possible" << endl;
        return;
    }
     
    int j = 0;
    while (j < N) {
         
        // Print elements from index k
        for (int i = k; i < N; i++)
            cout << mat[j][i] << " ";
             
        // Print elements before index k
        for (int i = 0; i < k; i++)
            cout << mat[j][i] << " ";
             
        cout << endl;
        j++;
    }
}
 
// Driver code
int main()
{
    int mat[N][N] = {{1, 2, 3, 4},
                     {5, 6, 7, 8},
                     {9, 10, 11, 12},
                     {13, 14, 15, 16}};
    int k = 2;
     
    // Function call
    shiftMatrixByK(mat, k);
     
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ implementation to sort the rows
// of matrix in ascending order followed by
// sorting the columns in descending order
#include <bits/stdc++.h>
using namespace std;
 
#define MAX_SIZE 10
 
// function to sort each row of the matrix
// according to the order specified by
// ascending.
void sortByRow(int mat[][MAX_SIZE], int n,
                           bool ascending)
{
    for (int i = 0; i < n; i++)
    {
      if (ascending)   
        sort(mat[i], mat[i] + n);
      else
          sort(mat[i], mat[i] + n, greater<int>());
    }     
}
 
// function to find transpose of the matrix
void transpose(int mat[][MAX_SIZE], int n)
{
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
 
            // swapping element at index (i, j)
            // by element at index (j, i)
            swap(mat[i][j], mat[j][i]);
}
 
// function to sort the matrix row-wise
// and column-wise
void sortMatRowAndColWise(int mat[][MAX_SIZE],
                                       int n)
{
    // sort rows of mat[][]
    sortByRow(mat, n, true);
 
    // get transpose of mat[][]
    transpose(mat, n);
 
    // again sort rows of mat[][] in descending
    // order.
    sortByRow(mat, n, false);
 
    // again get transpose of mat[][]
    transpose(mat, n);
}
 
// function to print the matrix
void printMat(int mat[][MAX_SIZE], int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << mat[i][j] << " ";
        cout << endl;
    }
}
 
// Driver program to test above
int main()
{
    int n = 3;
     
    int mat[n][MAX_SIZE]  = {{3, 2, 1},
                            {9, 8, 7},
                            {6, 5, 4}};
 
    cout << "Original Matrix:\n";
    printMat(mat, n);
 
    sortMatRowAndColWise(mat, n);
 
    cout << "\nMatrix After Sorting:\n";
    printMat(mat, n);
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant