Banana-Leopard/TASTY huggingface dataset:
// Iterative C++ program to reverse a linked list
#include <bits/stdc++.h>
using namespace std;
 
/* Link list node */
struct Node {
    int data;
    struct Node* next;
    Node(int data)
    {
        this->data = data;
        next = NULL;
    }
};
 
struct LinkedList {
    Node* head;
    LinkedList() { head = NULL; }
 
    /* Function to reverse the linked list */
    void reverse()
    {
        // Initialize current, previous and next pointers
        Node* current = head;
        Node *prev = NULL, *next = NULL;
 
        while (current != NULL) {
            // Store next
            next = current->next;
            // Reverse current node's pointer
            current->next = prev;
            // Move pointers one position ahead.
            prev = current;
            current = next;
        }
        head = prev;
    }
 
    /* Function to print linked list */
    void print()
    {
        struct Node* temp = head;
        while (temp != NULL) {
            cout << temp->data << " ";
            temp = temp->next;
        }
    }
 
    void push(int data)
    {
        Node* temp = new Node(data);
        temp->next = head;
        head = temp;
    }
};
 
/* Driver code*/
int main()
{
    /* Start with the empty list */
    LinkedList ll;
    ll.push(20);
    ll.push(4);
    ll.push(15);
    ll.push(85);
 
    cout << "Given linked list\n";
    ll.print();
 
    ll.reverse();
 
    cout << "\nReversed linked list \n";
    ll.print();
    return 0;
}

Time complexity: linear
Space complexity: constant
// Recursive C++ program to reverse
// a linked list
#include <bits/stdc++.h>
using namespace std;
 
/* Link list node */
struct Node {
    int data;
    struct Node* next;
    Node(int data)
    {
        this->data = data;
        next = NULL;
    }
};
 
struct LinkedList {
    Node* head;
    LinkedList() { head = NULL; }
 
    Node* reverse(Node* head)
    {
        if (head == NULL || head->next == NULL)
            return head;
 
        /* reverse the rest list and put
          the first element at the end */
        Node* rest = reverse(head->next);
        head->next->next = head;
 
        /* tricky step -- see the diagram */
        head->next = NULL;
 
        /* fix the head pointer */
        return rest;
    }
 
    /* Function to print linked list */
    void print()
    {
        struct Node* temp = head;
        while (temp != NULL) {
            cout << temp->data << " ";
            temp = temp->next;
        }
    }
 
    void push(int data)
    {
        Node* temp = new Node(data);
        temp->next = head;
        head = temp;
    }
};
 
/* Driver program to test above function*/
int main()
{
    /* Start with the empty list */
    LinkedList ll;
    ll.push(20);
    ll.push(4);
    ll.push(15);
    ll.push(85);
 
    cout << "Given linked list\n";
    ll.print();
 
    ll.head = ll.reverse(ll.head);
 
    cout << "\nReversed linked list \n";
    ll.print();
    return 0;
}

Time complexity: linear
Space complexity: linear
// A simple and tail recursive C++ program to reverse
// a linked list
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    struct Node* next;
};
 
void reverseUtil(Node* curr, Node* prev, Node** head);
 
// This function mainly calls reverseUtil()
// with prev as NULL
void reverse(Node** head)
{
    if (!head)
        return;
    reverseUtil(*head, NULL, head);
}
 
// A simple and tail-recursive function to reverse
// a linked list.  prev is passed as NULL initially.
void reverseUtil(Node* curr, Node* prev, Node** head)
{
    /* If last node mark it head*/
    if (!curr->next) {
        *head = curr;
        /* Update next to prev node */
        curr->next = prev;
        return;
    }
    /* Save curr->next node for recursive call */
    Node* next = curr->next;
    /* and update next ..*/
    curr->next = prev;
    reverseUtil(next, curr, head);
}
 
// A utility function to create a new node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->data = key;
    temp->next = NULL;
    return temp;
}
 
// A utility function to print a linked list
void printlist(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
    cout << endl;
}
 
// Driver code
int main()
{
    Node* head1 = newNode(1);
    head1->next = newNode(2);
    head1->next->next = newNode(3);
    head1->next->next->next = newNode(4);
    head1->next->next->next->next = newNode(5);
    head1->next->next->next->next->next = newNode(6);
    head1->next->next->next->next->next->next = newNode(7);
    head1->next->next->next->next->next->next->next
        = newNode(8);
    cout << "Given linked list\n";
    printlist(head1);
    reverse(&head1);
    cout << "Reversed linked list\n";
    printlist(head1);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: linear
// C++ program for above approach
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
// Create a class Node to enter values and address in the
// list
class Node {
public:
    int data;
    Node* next;
};
 
// Function to reverse the linked list
void reverseLL(Node** head)
{
    // Create a stack "s" of Node type
    stack<Node*> s;
    Node* temp = *head;
    while (temp->next != NULL) {
        // Push all the nodes in to stack
        s.push(temp);
        temp = temp->next;
    }
    *head = temp;
    while (!s.empty()) {
        // Store the top value of stack in list
        temp->next = s.top();
        // Pop the value from stack
        s.pop();
        // update the next pointer in the list
        temp = temp->next;
    }
    temp->next = NULL;
}
 
// Function to Display the elements in List
void printlist(Node* temp)
{
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
}
 
// Program to insert back of the linked list
void insert_back(Node** head, int value)
{
 
    // we have used insertion at back method to enter values
    // in the list.(eg: head->1->2->3->4->Null)
    Node* temp = new Node();
    temp->data = value;
    temp->next = NULL;
 
    // If *head equals to NULL
    if (*head == NULL) {
        *head = temp;
        return;
    }
    else {
        Node* last_node = *head;
        while (last_node->next != NULL)
            last_node = last_node->next;
        last_node->next = temp;
        return;
    }
}
 
// Driver Code
int main()
{
    Node* head = NULL;
    insert_back(&head, 1);
    insert_back(&head, 2);
    insert_back(&head, 3);
    insert_back(&head, 4);
    cout << "Given linked list\n";
    printlist(head);
    reverseLL(&head);
    cout << "\nReversed linked list\n";
    printlist(head);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: linear
// C++ program to delete a given key from
// linked list.
#include <bits/stdc++.h>
using namespace std;
 
// Structure for a node
class Node {
public:
    int data;
    Node* next;
};
 
// Function to insert a node at the
// beginning of a Circular linked list
void push(Node** head_ref, int data)
{
 
    // Create a new node and make head
    // as next of it.
    Node* ptr1 = new Node();
    ptr1->data = data;
    ptr1->next = *head_ref;
 
    // If linked list is not NULL then
    // set the next of last node
    if (*head_ref != NULL) {
 
        // Find the node before head and
        // update next of it.
        Node* temp = *head_ref;
        while (temp->next != *head_ref)
            temp = temp->next;
        temp->next = ptr1;
    }
    else
 
        // For the first node
        ptr1->next = ptr1;
 
    *head_ref = ptr1;
}
 
// Function to print nodes in a given
// circular linked list
void printList(Node* head)
{
    Node* temp = head;
    if (head != NULL) {
        do {
            cout << temp->data << " ";
            temp = temp->next;
        } while (temp != head);
    }
 
    cout << endl;
}
 
// Function to delete a given node
// from the list
void deleteNode(Node** head, int key)
{
 
    // If linked list is empty
    if (*head == NULL)
        return;
 
    // If the list contains only a
    // single node
    if ((*head)->data == key && (*head)->next == *head) {
        free(*head);
        *head = NULL;
        return;
    }
 
    Node *last = *head, *d;
 
    // If head is to be deleted
    if ((*head)->data == key) {
 
        // Find the last node of the list
        while (last->next != *head)
            last = last->next;
 
        // Point last node to the next of
        // head i.e. the second node
        // of the list
        last->next = (*head)->next;
        free(*head);
        *head = last->next;
        return;
    }
 
    // Either the node to be deleted is
    // not found or the end of list
    // is not reached
    while (last->next != *head && last->next->data != key) {
        last = last->next;
    }
 
    // If node to be deleted was found
    if (last->next->data == key) {
        d = last->next;
        last->next = d->next;
        free(d);
    }
    else
        cout << "no such keyfound";
}
 
// Driver code
int main()
{
    // Initialize lists as empty
    Node* head = NULL;
 
    // Created linked list will be
    // 2->5->7->8->10
    push(&head, 2);
    push(&head, 5);
    push(&head, 7);
    push(&head, 8);
    push(&head, 10);
 
    cout << "List Before Deletion: ";
    printList(head);
 
    deleteNode(&head, 7);
 
    cout << "List After Deletion: ";
    printList(head);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to check if linked list is circular
 
#include<bits/stdc++.h>
using namespace std;
 
/* Link list Node */
struct Node
{
    int data;
    struct Node* next;
};
 
/* This function returns true if given linked
   list is circular, else false. */
bool isCircular(struct Node *head)
{
    // An empty linked list is circular
    if (head == NULL)
       return true;
 
    // Next of head
    struct Node *node = head->next;
 
    // This loop would stop in both cases (1) If
    // Circular (2) Not circular
    while (node != NULL && node != head)
       node = node->next;
 
    // If loop stopped because of circular
    // condition
    return (node == head);
}
 
// Utility function to create a new node.
Node *newNode(int data)
{
    struct Node *temp = new Node;
    temp->data = data;
    temp->next = NULL;
    return temp;
}
 
// Driver's code
int main()
{
    /* Start with the empty list */
    struct Node* head = newNode(1);
    head->next = newNode(2);
    head->next->next = newNode(3);
    head->next->next->next = newNode(4);
 
    isCircular(head)? cout << "Yes\n" :
                      cout << "No\n" ;
 
    // Making linked list circular
    head->next->next->next->next = head;
 
    isCircular(head)? cout << "Yes\n" :
                      cout << "No\n" ;
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++  program to count number of nodes in a circular
// linked list.
#include <bits/stdc++.h>
using namespace std;
  
/*structure for a node*/
  
struct Node {
    int data;
    Node* next;
    Node(int x)
    {
        data = x;
        next = NULL;
    }
};
/* Function to insert a node at the beginning
of a Circular linked list */
struct Node* push(struct Node* last, int data)
{
    if (last == NULL) {
        struct Node* temp
            = (struct Node*)malloc(sizeof(struct Node));
  
        // Assigning the data.
        temp->data = data;
        last = temp;
        // Note : list was empty. We link single node
        // to itself.
        temp->next = last;
  
        return last;
    }
  
    // Creating a node dynamically.
    struct Node* temp
        = (struct Node*)malloc(sizeof(struct Node));
  
    // Assigning the data.
    temp->data = data;
  
    // Adjusting the links.
    temp->next = last->next;
    last->next = temp;
  
    return last;
}
  
/* Function to  count  nodes in a given Circular
linked list */
  
int countNodes(Node* head)
{
    Node* temp = head;
    int result = 0;
    if (head != NULL) {
        do {
            temp = temp->next;
            result++;
        } while (temp != head);
    }
  
    return result;
}
  
/* Driver program to test above functions */
int main()
{
    /* Initialize lists as empty */
    Node* head = NULL;
    head = push(head, 12);
    head = push(head, 56);
    head = push(head, 2);
    head = push(head, 11);
    cout << countNodes(head);
    return 0;
}
  
// This code is contributed by anushikaseth

Time complexity: linear
Space complexity: constant
// CPP program to exchange first and
// last node in circular linked list
#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int data;
    struct Node* next;
};
 
struct Node* addToEmpty(struct Node* head, int data)
{
    // This function is only for empty list
    if (head != NULL)
        return head;
 
    // Creating a node dynamically.
    struct Node* temp
        = (struct Node*)malloc(sizeof(struct Node));
 
    // Assigning the data.
    temp->data = data;
    head = temp;
 
    // Creating the link.
    head->next = head;
 
    return head;
}
 
struct Node* addBegin(struct Node* head, int data)
{
    if (head == NULL)
        return addToEmpty(head, data);
 
    struct Node* temp
        = (struct Node*)malloc(sizeof(struct Node));
 
    temp->data = data;
    temp->next = head->next;
    head->next = temp;
 
    return head;
}
 
/* function for traversing the list */
void traverse(struct Node* head)
{
    struct Node* p;
 
    // If list is empty, return.
    if (head == NULL) {
        cout << "List is empty." << endl;
        return;
    }
 
    // Pointing to first Node of the list.
    p = head;
 
    // Traversing the list.
    do {
        cout << p->data << " ";
        p = p->next;
 
    } while (p != head);
}
 
/* Function to exchange first and last node*/
struct Node* exchangeNodes(struct Node* head)
{
     
    // If list is of length less than 2
    if (head == NULL || head->next == NULL) {
        return head;
    }
    Node* tail = head;
   
    // Find pointer to the last node
    while (tail->next != head) {
        tail = tail->next;
    }
    /* Exchange first and last nodes using
       head and p */
   
    // temporary variable to store
    // head data
    int temp = tail->data;
    tail->data = head->data;
    head->data = temp;
    return head;
}
 
// Driven Program
int main()
{
    int i;
    struct Node* head = NULL;
    head = addToEmpty(head, 6);
 
    for (i = 5; i > 0; i--)
        head = addBegin(head, i);
    cout << "List Before: ";
    traverse(head);
    cout << endl;
 
    cout << "List After: ";
    head = exchangeNodes(head);
    traverse(head);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
class node {
public:
    node* prev;
    int data;
    node* next;
 
    node(int value)
    { // A constructor is called here
        prev = NULL; // By default previous pointer is
                     // pointed to NULL
        data = value; // value is assigned to the data
        next = NULL; // By default next pointer is pointed
                     // to NULL
    }
};
 
void insert_at_head(node*& head, int value)
{
 
    node* n = new node(value);
    n->next = head;
 
    if (head != NULL) {
        head->prev = n;
    }
 
    head = n;
}
 
void insert_at_tail(node*& head, int value)
{
 
    if (head == NULL) {
        insert_at_head(head, value);
        return;
    }
 
    node* n = new node(value);
    node* temp = head;
 
    while (temp->next != NULL) {
        temp = temp->next;
    }
    temp->next = n;
    n->prev = temp;
}
 
void display(node* head)
{
    node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " --> ";
        temp = temp->next;
    }
    cout << "NULL" << endl;
}
 
// Driver code
int main()
{
    node* head
        = NULL; // declaring an empty doubly linked list
 
      // Function call
    insert_at_tail(head, 1);
    insert_at_tail(head, 2);
    insert_at_tail(head, 3);
    insert_at_tail(head, 4);
    insert_at_tail(head, 5);
 
    cout << "After insertion at tail: ";
    display(head);
 
    cout << "After insertion at head: ";
    insert_at_head(head, 0);
 
    display(head);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to delete a node from
// Doubly Linked List
#include <bits/stdc++.h>
using namespace std;
 
/* a node of the doubly linked list */
class Node
{
    public:
    int data;
    Node* next;
    Node* prev;
};
 
/* Function to delete a node in a Doubly Linked List.
head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
void deleteNode(Node** head_ref, Node* del)
{
    /* base case */
    if (*head_ref == NULL || del == NULL)
        return;
 
    /* If node to be deleted is head node */
    if (*head_ref == del)
        *head_ref = del->next;
 
    /* Change next only if node to be
    deleted is NOT the last node */
    if (del->next != NULL)
        del->next->prev = del->prev;
 
    /* Change prev only if node to be
    deleted is NOT the first node */
    if (del->prev != NULL)
        del->prev->next = del->next;
 
    /* Finally, free the memory occupied by del*/
    free(del);
    return;
}
 
/* UTILITY FUNCTIONS */
/* Function to insert a node at the
beginning of the Doubly Linked List */
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = new Node();
 
    /* put in the data */
    new_node->data = new_data;
 
    /* since we are adding at the beginning,
    prev is always NULL */
    new_node->prev = NULL;
 
    /* link the old list of the new node */
    new_node->next = (*head_ref);
 
    /* change prev of head node to new node */
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
 
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
/* Function to print nodes in a given doubly linked list
This function is same as printList() of singly linked list */
void printList(Node* node)
{
    while (node != NULL)
    {
        cout << node->data << " ";
        node = node->next;
    }
}
 
/* Driver code*/
int main()
{
    /* Start with the empty list */
    Node* head = NULL;
 
    /* Let us create the doubly linked list 10<->8<->4<->2 */
    push(&head, 2);
    push(&head, 4);
    push(&head, 8);
    push(&head, 10);
 
    cout << "Original Linked list ";
    printList(head);
 
    /* delete nodes from the doubly linked list */
    deleteNode(&head, head); /*delete first node*/
    deleteNode(&head, head->next); /*delete middle node*/
    deleteNode(&head, head->next); /*delete last node*/
 
    /* Modified linked list will be NULL<-8->NULL */
    cout << "\nModified Linked list ";
    printList(head);
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: constant
Space complexity: constant
/* C++ program to reverse a doubly linked list */
 
#include <bits/stdc++.h>
using namespace std;
 
/* Node of the doubly linked list */
class Node {
public:
    int data;
    Node* next;
    Node* prev;
};
 
/* Function to reverse a Doubly Linked List */
void reverse(Node** head_ref)
{
    Node* temp = NULL;
    Node* current = *head_ref;
 
    /* swap next and prev for all nodes of
    doubly linked list */
    while (current != NULL) {
        temp = current->prev;
        current->prev = current->next;
        current->next = temp;
        current = current->prev;
    }
 
    /* Before changing the head, check for the cases like
       empty list and list with only one node */
    if (temp != NULL)
        *head_ref = temp->prev;
}
 
/* UTILITY FUNCTIONS */
/* Function to insert a node at the
beginning of the Doubly Linked List */
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = new Node();
 
    /* put in the data */
    new_node->data = new_data;
 
    /* since we are adding at the beginning,
    prev is always NULL */
    new_node->prev = NULL;
 
    /* link the old list of the new node */
    new_node->next = (*head_ref);
 
    /* change prev of head node to new node */
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
 
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
/* Function to print nodes in a given doubly linked list
This function is same as printList() of singly linked list
*/
void printList(Node* node)
{
    while (node != NULL) {
        cout << node->data << " ";
        node = node->next;
    }
}
 
// Driver's code
int main()
{
    /* Start with the empty list */
    Node* head = NULL;
 
    /* Let us create a sorted linked list to test the
    functions Created linked list will be 10->8->4->2 */
    push(&head, 2);
    push(&head, 4);
    push(&head, 8);
    push(&head, 10);
 
    cout << "Original Linked list" << endl;
    printList(head);
 
    // Function call
    reverse(&head);
 
    cout << "\nReversed Linked list" << endl;
    printList(head);
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: constant
// C++ program to reverse a doubly linked list
#include <bits/stdc++.h>
using namespace std;
struct LinkedList {
    struct Node {
        int data;
        Node *next, *prev;
        Node(int d)
        {
            data = d;
            next = prev = NULL;
        }
    };
    Node* head = NULL;
 
    /* Function to reverse a Doubly Linked List using Stacks
     */
    void reverse()
    {
        stack<int> st;
        Node* temp = head;
        while (temp != NULL) {
            st.push(temp->data);
            temp = temp->next;
        }
 
        // added all the elements sequence wise in the
        // st
        temp = head;
        while (temp != NULL) {
            temp->data = st.top();
            st.pop();
            temp = temp->next;
        }
 
        // popped all the elements and the added in the
        // linked list,
        // which are in the reversed order->
    }
 
    /* UTILITY FUNCTIONS */
    /* Function to insert a node at the beginning of the
     * Doubly Linked List */
    void Push(int new_data)
    {
 
        /* allocate node */
        Node* new_node = new Node(new_data);
 
        /* since we are adding at the beginning,
         prev is always NULL */
        new_node->prev = NULL;
 
        /* link the old list of 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;
    }
 
    /* Function to print nodes in a given doubly linked list
     This function is same as printList() of singly linked
     list */
    void printList(Node* node)
    {
        while (node) {
            cout << node->data << " ";
            node = node->next;
        }
    }
};
 
// Driver Code
int main()
{
    LinkedList list;
 
    /* Let us create a sorted linked list to test the
     functions Created linked list will be 10->8->4->2
   */
    list.Push(2);
    list.Push(4);
    list.Push(8);
    list.Push(10);
    cout << "Original linked list " << endl;
    list.printList(list.head);
    list.reverse();
    cout << endl;
    cout << "The reversed Linked List is " << endl;
    list.printList(list.head);
}
 
// This code is contributed by Pratham76

Time complexity: linear
Space complexity: linear