Banana-Leopard/TASTY huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
 
class Node {
public:
    int data;
    Node(int d) { data = d; }
    Node* ptr;
};
 
// Function to check if the linked list
// is palindrome or not
bool isPalin(Node* head)
{
 
    // Temp pointer
    Node* slow = head;
 
    // Declare a stack
    stack<int> s;
 
    // Push all elements of the list
    // to the stack
    while (slow != NULL) {
        s.push(slow->data);
 
        // Move ahead
        slow = slow->ptr;
    }
 
    // Iterate in the list again and
    // check by popping from the stack
    while (head != NULL) {
 
        // Get the top most element
        int i = s.top();
 
        // Pop the element
        s.pop();
 
        // Check if data is not
        // same as popped element
        if (head->data != i) {
            return false;
        }
 
        // Move ahead
        head = head->ptr;
    }
 
    return true;
}
 
// Driver Code
int main()
{
 
    // Addition of linked list
    Node one = Node(1);
    Node two = Node(2);
    Node three = Node(3);
    Node four = Node(2);
    Node five = Node(1);
 
    // Initialize the next pointer
    // of every current pointer
    five.ptr = NULL;
    one.ptr = &two
    two.ptr = &three
    three.ptr = &four
    four.ptr = &five
    Node* temp = &one
 
    // Call function to check palindrome or not
    int result = isPalin(&one);
 
    if (result == 1)
        cout << "isPalindrome is true\n";
    else
        cout << "isPalindrome is false\n";
 
    return 0;
}
 
// This code has been contributed by Striver

Time complexity: linear
Space complexity: linear
// Recursive program to check if a given linked list is
// palindrome
#include <bits/stdc++.h>
using namespace std;
 
/* Link list node */
struct node {
    char data;
    struct node* next;
};
 
// Initial parameters to this function are &head and head
bool isPalindromeUtil(struct node** left,
                      struct node* right)
{
    /* stop recursion when right becomes NULL */
    if (right == NULL)
        return true;
 
    /* If sub-list is not palindrome then no need to
    check for current left and right, return false */
    bool isp = isPalindromeUtil(left, right->next);
    if (isp == false)
        return false;
 
    /* Check values at current left and right */
    bool isp1 = (right->data == (*left)->data);
 
    /* Move left to next node */
    *left = (*left)->next;
 
    return isp1;
}
 
// A wrapper over isPalindromeUtil()
bool isPalindrome(struct node* head)
{
    return isPalindromeUtil(&head, head);
}
 
/* Push a node to linked list. Note that this function
changes the head */
void push(struct node** head_ref, char 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 off the new node */
    new_node->next = (*head_ref);
 
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
// A utility function to print a given linked list
void printList(struct node* ptr)
{
    while (ptr != NULL) {
        cout << ptr->data << "->";
        ptr = ptr->next;
    }
    cout << "NULL\n";
}
 
/* Driver program to test above function*/
int main()
{
    /* Start with the empty list */
    struct node* head = NULL;
    char str[] = "abacaba";
    int i;
 
    for (i = 0; str[i] != '\0'; i++) {
        push(&head, str[i]);
    }
    isPalindrome(head) ? cout << "Is Palindrome\n\n"
                       : cout << "Not Palindrome\n\n";
    return 0;
}
// this code is contributed by shivanisinghss2110

Time complexity: linear
Space complexity: linear
/* C++ Program to remove duplicates in an unsorted
   linked list */
#include <bits/stdc++.h>
using namespace std;
 
/* A linked list node */
struct Node {
    int data;
    struct Node* next;
};
 
// Utility function to create a new Node
struct Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->next = NULL;
    return temp;
}
 
/* Function to remove duplicates from a
   unsorted linked list */
void removeDuplicates(struct Node* start)
{
    struct Node *ptr1, *ptr2, *dup;
    ptr1 = start;
 
    /* Pick elements one by one */
    while (ptr1 != NULL && ptr1->next != NULL) {
        ptr2 = ptr1;
 
        /* Compare the picked element with rest
           of the elements */
        while (ptr2->next != NULL) {
            /* If duplicate then delete it */
            if (ptr1->data == ptr2->next->data) {
                /* sequence of steps is important here */
                dup = ptr2->next;
                ptr2->next = ptr2->next->next;
                delete (dup);
            }
            else /* This is tricky */
                ptr2 = ptr2->next;
        }
        ptr1 = ptr1->next;
    }
}
 
/* Function to print nodes in a given linked list */
void printList(struct Node* node)
{
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}
 
// Driver code
int main()
{
    /* The constructed linked list is:
     10->12->11->11->12->11->10*/
    struct Node* start = newNode(10);
    start->next = newNode(12);
    start->next->next = newNode(11);
    start->next->next->next = newNode(11);
    start->next->next->next->next = newNode(12);
    start->next->next->next->next->next = newNode(11);
    start->next->next->next->next->next->next = newNode(10);
 
    printf("Linked list before removing duplicates ");
    printList(start);
 
    removeDuplicates(start);
 
    printf("\nLinked list after removing duplicates ");
    printList(start);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
/* This program swaps the nodes of linked list rather
than swapping the field from the nodes.*/
#include <bits/stdc++.h>
using namespace std;
 
/* A linked list node */
class Node {
public:
    int data;
    Node* next;
};
 
/* Function to swap nodes x and y in linked list by
changing links */
void swapNodes(Node** head_ref, int x, int y)
{
    // Nothing to do if x and y are same
    if (x == y)
        return;
 
    // Search for x (keep track of prevX and CurrX
    Node *prevX = NULL, *currX = *head_ref;
    while (currX && currX->data != x) {
        prevX = currX;
        currX = currX->next;
    }
 
    // Search for y (keep track of prevY and CurrY
    Node *prevY = NULL, *currY = *head_ref;
    while (currY && currY->data != y) {
        prevY = currY;
        currY = currY->next;
    }
 
    // If either x or y is not present, nothing to do
    if (currX == NULL || currY == NULL)
        return;
 
    // If x is not head of linked list
    if (prevX != NULL)
        prevX->next = currY;
    else // Else make y as new head
        *head_ref = currY;
 
    // If y is not head of linked list
    if (prevY != NULL)
        prevY->next = currX;
    else // Else make x as new head
        *head_ref = currX;
 
    // Swap next pointers
    Node* temp = currY->next;
    currY->next = currX->next;
    currX->next = temp;
}
 
/* Function to add a node at the beginning of 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;
 
    /* link the old list off the new node */
    new_node->next = (*head_ref);
 
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
/* Function to print nodes in a given linked list */
void printList(Node* node)
{
    while (node != NULL) {
        cout << node->data << " ";
        node = node->next;
    }
}
 
/* Driver program to test above function */
int main()
{
    Node* start = NULL;
 
    /* The constructed linked list is:
    1->2->3->4->5->6->7 */
    push(&start, 7);
    push(&start, 6);
    push(&start, 5);
    push(&start, 4);
    push(&start, 3);
    push(&start, 2);
    push(&start, 1);
 
    cout << "Linked list before calling swapNodes() ";
    printList(start);
 
    swapNodes(&start, 4, 3);
 
    cout << "\nLinked list after calling swapNodes() ";
    printList(start);
 
    return 0;
}
 
// This is code is contributed by rathbhupendra

Time complexity: linear
Space complexity: constant
// C++ program to swap two given nodes of a linked list
#include <iostream>
using namespace std;
 
// A linked list node class
class Node {
public:
    int data;
    class Node* next;
    // constructor
    Node(int val, Node* next)
        : data(val)
        , next(next)
    {
    }
    // print list from this to last till null
    void printList()
    {
        Node* node = this;
        while (node != NULL) {
            cout << node->data << " ";
            node = node->next;
        }
        cout << endl;
    }
};
 
// Function to add a node at the beginning of List
void push(Node** head_ref, int new_data)
{
    // allocate node
    (*head_ref) = new Node(new_data, *head_ref);
}
 
void swap(Node*& a, Node*& b)
{
    Node* temp = a;
    a = b;
    b = temp;
}
 
void swapNodes(Node** head_ref, int x, int y)
{
    // Nothing to do if x and y are same
    if (x == y)
        return;
    Node **a = NULL, **b = NULL;
    // search for x and y in the linked list
    // and store their pointer in a and b
    while (*head_ref) {
        if ((*head_ref)->data == x)
            a = head_ref;
        else if ((*head_ref)->data == y)
            b = head_ref;
        head_ref = &((*head_ref)->next);
    }
    // if we have found both a and b
    // in the linked list swap current
    // pointer and next pointer of these
    if (a && b) {
        swap(*a, *b);
        swap(((*a)->next), ((*b)->next));
    }
}
 
// Driver code
int main()
{
    Node* start = NULL;
    // The constructed linked list is:
    // 1->2->3->4->5->6->7
    push(&start, 7);
    push(&start, 6);
    push(&start, 5);
    push(&start, 4);
    push(&start, 3);
    push(&start, 2);
    push(&start, 1);
 
    cout << "Linked list before calling swapNodes() ";
    start->printList();
 
    swapNodes(&start, 6, 1);
 
    cout << "Linked list after calling swapNodes() ";
    start->printList();
}
 
// This code is contributed by Sania Kumari Gupta
// (kriSania804)

Time complexity: linear
Space complexity: constant
/* CPP Program to move last element
to front in a given linked list */
 
#include <bits/stdc++.h>
using namespace std;
 
/* A linked list node */
class Node {
public:
    int data;
    Node* next;
};
 
/* We are using a double pointer
head_ref here because we change
head of the linked list inside
this function.*/
void moveToFront(Node** head_ref)
{
    /* If linked list is empty, or
    it contains only one node,
    then nothing needs to be done,
    simply return */
    if (*head_ref == NULL || (*head_ref)->next == NULL)
        return;
 
    /* Initialize second last
    and last pointers */
    Node* secLast = NULL;
    Node* last = *head_ref;
 
    /*After this loop secLast contains
    address of second last node and
    last contains address of last node in Linked List */
    while (last->next != NULL) {
        secLast = last;
        last = last->next;
    }
 
    /* Set the next of second last as NULL */
    secLast->next = NULL;
 
    /* Set next of last as head node */
    last->next = *head_ref;
 
    /* Change the head pointer
    to point to last node now */
    *head_ref = last;
}
 
/* UTILITY FUNCTIONS */
/* Function to add a node
at the beginning of 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;
 
    /* link the old list off the new node */
    new_node->next = (*head_ref);
 
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
/* Function to print nodes in a given linked list */
void printList(Node* node)
{
    while (node != NULL) {
        cout << node->data << " ";
        node = node->next;
    }
}
 
// Driver's code
int main()
{
    Node* start = NULL;
 
    /* The constructed linked list is:
    1->2->3->4->5 */
    push(&start, 5);
    push(&start, 4);
    push(&start, 3);
    push(&start, 2);
    push(&start, 1);
 
    cout << "Linked list before moving last to front\n";
    printList(start);
 
      // Function call
    moveToFront(&start);
 
    cout << "\nLinked list after removing last to front\n";
    printList(start);
 
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: linear
Space complexity: constant
#include<bits/stdc++.h>
using namespace std;
 
/* Link list node */
struct Node {
    int data;
    Node* next;
};
 
void push(Node** head_ref, int new_data);
 
/*This solution uses the temporary
 dummy to build up the result list */
Node* sortedIntersect(Node* a, Node* b)
{
    Node dummy;
    Node* tail = &dummy
    dummy.next = NULL;
 
    /* Once one or the other
    list runs out -- we're done */
    while (a != NULL && b != NULL) {
        if (a->data == b->data) {
            push((&tail->next), a->data);
            tail = tail->next;
            a = a->next;
            b = b->next;
        }
        /* advance the smaller list */
        else if (a->data < b->data)
            a = a->next;
        else
            b = b->next;
    }
    return (dummy.next);
}
 
/* UTILITY FUNCTIONS */
/* Function to insert a node at
the beginning of the linked list */
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = (Node*)malloc(
        sizeof(Node));
 
    /* put in the data  */
    new_node->data = new_data;
 
    /* link the old list 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 nodes in
   a given linked list */
void printList(Node* node)
{
    while (node != NULL) {
        cout << node->data <<" ";
        node = node->next;
    }
}
 
/* Driver program to test above functions*/
int main()
{
    /* Start with the empty lists */
    Node* a = NULL;
    Node* b = NULL;
    Node* intersect = NULL;
 
    /* Let us create the first sorted
     linked list to test the functions
     Created linked list will be
     1->2->3->4->5->6 */
    push(&a, 6);
    push(&a, 5);
    push(&a, 4);
    push(&a, 3);
    push(&a, 2);
    push(&a, 1);
 
    /* Let us create the second sorted linked list
   Created linked list will be 2->4->6->8 */
    push(&b, 8);
    push(&b, 6);
    push(&b, 4);
    push(&b, 2);
 
    /* Find the intersection two linked lists */
    intersect = sortedIntersect(a, b);
 
    cout<<"Linked list containing common items of a & b \n";
    printList(intersect);
}

Time complexity: linear
Space complexity: linear
// C++ program to implement above approach
#include <bits/stdc++.h>
 
/* Link list node */
struct Node {
    int data;
    struct Node* next;
};
 
void push(struct Node** head_ref,
          int new_data);
 
/* This solution uses the local reference */
struct Node* sortedIntersect(
    struct Node* a,
    struct Node* b)
{
    struct Node* result = NULL;
    struct Node** lastPtrRef = &result
 
    /* Advance comparing the first
     nodes in both lists.
    When one or the other list runs
     out, we're done. */
    while (a != NULL && b != NULL) {
        if (a->data == b->data) {
            /* found a node for the intersection */
            push(lastPtrRef, a->data);
            lastPtrRef = &((*lastPtrRef)->next);
            a = a->next;
            b = b->next;
        }
        else if (a->data < b->data)
            a = a->next; /* advance the smaller list */
        else
            b = b->next;
    }
    return (result);
}
 
/* UTILITY FUNCTIONS */
/* 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 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 nodes in a given linked list */
void printList(struct Node* node)
{
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}
 
/* Driver program to test above functions*/
int main()
{
    /* Start with the empty lists */
    struct Node* a = NULL;
    struct Node* b = NULL;
    struct Node* intersect = NULL;
 
    /* Let us create the first sorted
     linked list to test the functions
   Created linked list will be
   1->2->3->4->5->6 */
    push(&a, 6);
    push(&a, 5);
    push(&a, 4);
    push(&a, 3);
    push(&a, 2);
    push(&a, 1);
 
    /* Let us create the second sorted linked list
   Created linked list will be 2->4->6->8 */
    push(&b, 8);
    push(&b, 6);
    push(&b, 4);
    push(&b, 2);
 
    /* Find the intersection two linked lists */
    intersect = sortedIntersect(a, b);
 
    printf("\n Linked list containing common items of a & b \n ");
    printList(intersect);
 
    return 0;
}
 
//This code is contributed by Abhijeet Kumar(abhijeet19403)

Time complexity: linear
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
// Link list node
struct Node
{
    int data;
    struct Node* next;
};
 
struct Node* sortedIntersect(struct Node* a,
                             struct Node* b)
{
     
    // base case
    if (a == NULL || b == NULL)
        return NULL;
 
    /* If both lists are non-empty */
 
    /* Advance the smaller list and
       call recursively */
    if (a->data < b->data)
        return sortedIntersect(a->next, b);
 
    if (a->data > b->data)
        return sortedIntersect(a, b->next);
 
    // Below lines are executed only
    // when a->data == b->data
    struct Node* temp = (struct Node*)malloc(
                  sizeof(struct Node));
    temp->data = a->data;
 
    // Advance both lists and call recursively
    temp->next = sortedIntersect(a->next,
                                 b->next);
    return temp;
}
 
/* UTILITY FUNCTIONS */
/* 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 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 nodes in
   a given linked list */
void printList(struct Node* node)
{
    while (node != NULL)
    {
        cout << " " << node->data;
        node = node->next;
    }
}
 
// Driver code
int main()
{
     
    /* Start with the empty lists */
    struct Node* a = NULL;
    struct Node* b = NULL;
    struct Node* intersect = NULL;
 
    /* Let us create the first sorted
      linked list to test the functions
     Created linked list will be
      1->2->3->4->5->6 */
    push(&a, 6);
    push(&a, 5);
    push(&a, 4);
    push(&a, 3);
    push(&a, 2);
    push(&a, 1);
 
    /* Let us create the second sorted linked list
     Created linked list will be 2->4->6->8 */
    push(&b, 8);
    push(&b, 6);
    push(&b, 4);
    push(&b, 2);
 
    /* Find the intersection two linked lists */
    intersect = sortedIntersect(a, b);
 
    cout << "\n Linked list containing "
         << "common items of a & b \n ";
    printList(intersect);
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110

Time complexity: linear
Space complexity: linear
// C++ program to implement above approach
#include <bits/stdc++.h>
using namespace std;
 
// Link list node
struct Node {
    int data;
    struct Node* next;
};
void printList(struct Node* node)
{
    while (node != NULL) {
        cout << " " << node->data;
        node = node->next;
    }
}
 
void append(struct Node** head_ref, int new_data)
{
 
    struct Node* new_node
        = (struct Node*)malloc(sizeof(struct Node));
 
    new_node->data = new_data;
 
    new_node->next = (*head_ref);
 
    (*head_ref) = new_node;
}
vector<int> intersection(struct Node* tmp1, struct Node* tmp2,
                  int k)
{
    vector<int> res(k);
    unordered_set<int> set;
    while (tmp1 != NULL) {
 
        set.insert(tmp1->data);
        tmp1 = tmp1->next;
    }
 
    int cnt = 0;
 
    while (tmp2 != NULL) {
        if (set.find(tmp2->data) != set.end()) {
            res[cnt] = tmp2->data;
            cnt++;
        }
        tmp2 = tmp2->next;
    }
 
    return res;
}
 
// Driver code
int main()
{
 
    struct Node* ll = NULL;
    struct Node* ll1 = NULL;
 
    append(≪,7);
    append(≪,6);
    append(≪,5);
    append(≪,4);
    append(≪,3);
    append(≪,2);
    append(≪,1);
    append(≪,0);
 
    append(&ll1,7);
    append(&ll1,6);
    append(&ll1,5);
    append(&ll1,4);
    append(&ll1,3);
    append(&ll1,12);
    append(&ll1,0);
    append(&ll1,9);
 
    vector<int> arr= intersection(ll, ll1, 6);
 
    for (int i :arr)
        cout << i << "\n";
 
    return 0;
}
 
// This code is contributed by Abhijeet Kumar(abhijeet19403)

Time complexity: linear
Space complexity: linear
// CPP program to segregate even and odd nodes in a
// Linked List
#include <bits/stdc++.h>
using namespace std;
 
/* a node of the singly linked list */
struct Node {
    int data;
    struct Node* next;
};
 
// Function to segregate even and odd nodes.
void segregateEvenOdd(struct Node** head_ref)
{
    // Starting node of list having even values.
    Node* evenStart = NULL;
    // Ending node of even values list.
    Node* evenEnd = NULL;
    // Starting node of odd values list.
    Node* oddStart = NULL;
    // Ending node of odd values list.
    Node* oddEnd = NULL;
    // Node to traverse the list.
    Node* currNode = *head_ref;
 
    while (currNode != NULL) {
        int val = currNode->data;
 
        // If current value is even, add it to even values
        // list.
        if (val % 2 == 0) {
            if (evenStart == NULL) {
                evenStart = currNode;
                evenEnd = evenStart;
            }
            else {
                evenEnd->next = currNode;
                evenEnd = evenEnd->next;
            }
        }
 
        // If current value is odd, add it to odd values
        // list.
        else {
            if (oddStart == NULL) {
                oddStart = currNode;
                oddEnd = oddStart;
            }
            else {
                oddEnd->next = currNode;
                oddEnd = oddEnd->next;
            }
        }
 
        // Move head pointer one step in forward direction
        currNode = currNode->next;
    }
 
    // If either odd list or even list is empty, no change
    // is required as all elements are either even or odd.
    if (oddStart == NULL || evenStart == NULL)
        return;
 
    // Add odd list after even list.
    evenEnd->next = oddStart;
    oddEnd->next = NULL;
 
    // Modify head pointer to starting of even list.
    *head_ref = evenStart;
}
 
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning */
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = new Node();
    /* put in the data */
    new_node->data = new_data;
    /* link the old list off the new node */
    new_node->next = (*head_ref);
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
 
/* Function to print nodes in a given linked list */
void printList(Node* node)
{
    while (node != NULL) {
        cout << node->data << " ";
        node = node->next;
    }
}
 
/* Driver program to test above functions*/
int main()
{
    /* Start with the empty list */
    Node* head = NULL;
 
    /* Let us create a sample linked list as following
    0->1->4->6->9->10->11 */
 
    push(&head, 11);
    push(&head, 10);
    push(&head, 9);
    push(&head, 6);
    push(&head, 4);
    push(&head, 1);
    push(&head, 0);
 
    cout << "Original Linked list" << endl;
    printList(head);
 
    segregateEvenOdd(&head);
 
    cout << endl << "Modified Linked list " << endl;
    printList(head);
 
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: constant