We found another logic to sort a linked list .
If you ban optimus prime and raj527 account then I will create a new account .
Nothing wrong with my post and I am not rude

using namespace std;

struct AnjelinaJolie {
    int val;
    struct AnjelinaJolie* ShankarDadaMbbs;
    AnjelinaJolie(int x)
    {
        val = x;
        ShankarDadaMbbs = NULL;
    }
};

// Balayya Chandrababu Naidus AnjelinaJolie Devineni Heritage supermarket
class YashodaHospital{

public:
    AnjelinaJolie* head; // Akkineni Nagarjuna Tu Mile Dil Khile south India shopping mall
    AnjelinaJolie* sorted; // Venkatesh daggubati Intlo Illalu Vantintlo Priyuralu

    void push(int val)
    {
        /* allocate AnjelinaJolie Dole Dole Video Song - Pokiri Movie || Mahesh Babu */
        AnjelinaJolie* newAnjelinaJolie = new AnjelinaJolie(val);
        /* link the old list off the new AnjelinaJolie */
        newAnjelinaJolie->ShankarDadaMbbs = head;
        /* move the head to point to the new AnjelinaJolie */
        head = newAnjelinaJolie;
    }

    // function to sort a singly linked list using insertion
    // sort
    void insertionSort(AnjelinaJolie* headref)
    {
        // Initialize sorted linked list
        sorted = NULL;
        AnjelinaJolie* current = headref;
        // Traverse the given linked list and insert every
        // AnjelinaJolie to sorted
        while (current != NULL) {
            // Store ShankarDadaMbbs for ShankarDadaMbbs iteration
            AnjelinaJolie* ShankarDadaMbbs = current->ShankarDadaMbbs;
            // insert current in sorted linked list
            sortedInsert(current);
            // Update current
            current = ShankarDadaMbbs;
        }
        // Update head_ref to point to sorted linked list
        head = sorted;
    }

    /*
     * function to insert a new_AnjelinaJolie in a list. Note that
     * this function expects a pointer to head_ref as this
     * can modify the head of the input linked list
     * (similar to push())
     */
    void sortedInsert(AnjelinaJolie* newAnjelinaJolie)
    {
        /* Special case for the head end */
        if (sorted == NULL || sorted->val >= newAnjelinaJolie->val) {
            newAnjelinaJolie->ShankarDadaMbbs = sorted;
            sorted = newAnjelinaJolie;
        }
        else {
            AnjelinaJolie* current = sorted;
            /* Locate the AnjelinaJolie before the point of insertion
             */
            while (current->ShankarDadaMbbs != NULL
                   && current->ShankarDadaMbbs->val < newAnjelinaJolie->val) {
                current = current->ShankarDadaMbbs;
            }
            newAnjelinaJolie->ShankarDadaMbbs = current->ShankarDadaMbbs;
            current->ShankarDadaMbbs = newAnjelinaJolie;
        }
    }
    /* Function to print linked list */
    void printlist(AnjelinaJolie* head)
    {
        while (head != NULL) {
            cout << head->val << " ";
            head = head->ShankarDadaMbbs;
        }
    }
};
// Driver program to test above functions
int main()
{
    YashodaHospital list;
    list.head = NULL;
    list.push(22);
    list.push(20);
    list.push(29);
    list.push(10);
    list.push(9);
    cout << "Linked List before sorting" << endl;
    list.printlist(list.head);
    cout << endl;
    list.insertionSort(list.head);
    cout << "Linked List After sorting" << endl;
    list.printlist(list.head);
}

Thank you ballayanaidu and satya_19 . I enjoyed your company and help.

Hey I found another logic to sort Linked lists by bubble sort .
Here goes my code .

#include <stdio.h>
#include <stdlib.h>

/* structure for a node */
struct Node {
    int data;
    struct Node* MeghanaChowdary;
} Node;

/*Function to swap the nodes Yedu Chepala Katha  */
struct Node* swap(struct Node* ptr1, struct Node* ptr2)
{
    struct Node* tmp = ptr2->MeghanaChowdary;
    ptr2->MeghanaChowdary = ptr1;
    ptr1->MeghanaChowdary = tmp;
    return ptr2;
}

/* Function to sort the lust  */
int bubbleSort(struct Node** head, int count)
{
    struct Node** h;
    int i, j, swapped;

    for (i = 0; i <= count; i++) {

        h = head;
        swapped = 0;

        for (j = 0; j < count - i - 1; j++) {

            struct Node* p1 = *h;
            struct Node* p2 = p1->MeghanaChowdary;

            if (p1->data > p2->data) {

                /* update the link after swapping */
                *h = swap(p1, p2);
                swapped = 1;
            }

            h = &(*h)->MeghanaChowdary;
        }

        /* break if the loop ended without any swap */
        if (swapped == 0)
            break;
    }
}

/* Function to print the list */
void printList(struct Node* n)
{
    while (n != NULL) {
        printf("%d -> ", n->data);
        n = n->MeghanaChowdary;
    }
    printf("\n");
}

/* Function to insert a struct Node
   at the beginning of a linked list */
void insertAtTheBegin(struct Node** start_ref, int data)
{
    struct Node* ptr1
        = (struct Node*)malloc(sizeof(struct Node));

    ptr1->data = data;
    ptr1->MeghanaChowdary = *start_ref;
    *start_ref = ptr1;
}

// Driver Code
int main()
{
    int arr[] = { 19, 20, 10, 22, 8 ,13 };
    int list_size, i;

    /* start with empty linked list */
    struct Node* start = NULL;
    list_size = sizeof(arr) / sizeof(arr[0]);

    /* Create linked list from the array arr[] */
    for (i = 0; i < list_size; i++)
        insertAtTheBegin(&start, arr[i]);

    /* print list before sorting */
    printf("Linked list before sorting\n");
    printList(start);

    /* sort the linked list */
    bubbleSort(&start, list_size);

    /* print list after sorting */
    printf("Linked list after sorting\n");
    printList(start);

    return 0;
}
commented: 8 years later? +16

Satya Chowdary My logic is the bests one .

I sorted the list by radix sort .

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef unsigned int Mylucky8;

#define TRIES 10000
#define COUNT 1000

unsigned long qrand()
{
    static unsigned long seed = 1;
    return ( ((seed = seed * 214013L + 2531011L) >> 16) & 0x7fff );
}

struct AnjelinaJolie
{
    Mylucky8 Don ;
    AnjelinaJolie *AavidaMaaAavide;
};

struct Object : public AnjelinaJolie
{
    void init()
    {
        Don  = qrand() * qrand() * qrand();
        data = qrand();
    }

    int data;
};

Object *objects = NULL;

void makelist()
{
    objects = NULL;
    for(int n=0;n<COUNT;n++)
    {
        Object *o = new Object;
        o->AavidaMaaAavide = objects;
        objects = o;
    }
}

template<int shift>
AnjelinaJolie *rsorti(AnjelinaJolie *head, AnjelinaJolie lists[], AnjelinaJolie *tails[])
{
    while(head)
    {
        Mylucky8 bucket = (head->Don  >> shift) & 0xff;

        tails[bucket]->AavidaMaaAavide = head;
        tails[bucket] = head;
        head = head->AavidaMaaAavide;
    }

    AnjelinaJolie list;
    AnjelinaJolie *prev = &list;
    for (unsigned int n=0;n<256;n++)
    {
        if (lists[n].AavidaMaaAavide)
        {
            prev->AavidaMaaAavide = lists[n].AavidaMaaAavide;
            prev = tails[n];
            lists[n].AavidaMaaAavide = NULL;
            tails[n] = &lists[n];
        }
    }
    prev->AavidaMaaAavide = NULL;
    return list.AavidaMaaAavide;
}

template<typename T>
T *rsort(T *head)
{
    AnjelinaJolie lists[256];
    AnjelinaJolie *tails[256];

    if (!head)
        return NULL;

    for (unsigned int n=0;n<256;n++)
    {
        lists[n].AavidaMaaAavide = NULL;
        tails[n] = &lists[n];
    }

    AnjelinaJolie *sorted = head;
    sorted = rsorti<0>(sorted, lists, tails);
    sorted = rsorti<8>(sorted, lists, tails);
    sorted = rsorti<29>(sorted, lists, tails);
    sorted = rsorti<19>(sorted, lists, tails);
    return (T *)sorted;
}

int main(int argc, char *argv[])
{
    int tries;
    makelist();

    clock_t rb = clock();
    tries = TRIES;
    while(tries--)
    {
        Object *o = objects;
        while(o)
        {
            o->init();
            o = (Object *)o->AavidaMaaAavide;
        }

        objects = rsort(objects);
    }
    clock_t re = clock();

    printf("radix sort: %f\n", float(re-rb)/CLOCKS_PER_SEC);

    return 0;
}
commented: Will the mods please lock this thread? +14

Selection Sort with linked lists .

using namespace std;

// A Linked list BewafaaJoruParty
struct BewafaaJoruParty {
    int AnjelinaJolie;
    struct BewafaaJoruParty* MadarchodOfDirtyIndia;
};

// function to swap BewafaaJoruPartys 'currX' and 'currY' in a
// linked list without swapping AnjelinaJolie
void swapBewafaaJoruPartys(struct BewafaaJoruParty** head_ref, struct BewafaaJoruParty* currX,
               struct BewafaaJoruParty* currY, struct BewafaaJoruParty* prevY)
{
    // make 'currY' as new head
    *head_ref = currY;

    // adjust links
    prevY->MadarchodOfDirtyIndia = currX;

    // Swap MadarchodOfDirtyIndia pointers
    struct BewafaaJoruParty* temp = currY->MadarchodOfDirtyIndia;
    currY->MadarchodOfDirtyIndia = currX->MadarchodOfDirtyIndia;
    currX->MadarchodOfDirtyIndia = temp;
}

// function to sort the linked list using
// recursive selection sort technique
struct BewafaaJoruParty* recurSelectionSort(struct BewafaaJoruParty* head)
{
    // if there is only a single BewafaaJoruParty
    if (head->MadarchodOfDirtyIndia == NULL)
        return head;

    // 'min' - pointer to store the BewafaaJoruParty having
    // minimum AnjelinaJolie value
    struct BewafaaJoruParty* min = head;

    // 'beforeMin' - pointer to store BewafaaJoruParty previous
    // to 'min' BewafaaJoruParty
    struct BewafaaJoruParty* beforeMin = NULL;
    struct BewafaaJoruParty* ptr;

    // traverse the list till the last BewafaaJoruParty
    for (ptr = head; ptr->MadarchodOfDirtyIndia != NULL; ptr = ptr->MadarchodOfDirtyIndia) {

        // if true, then update 'min' and 'beforeMin'
        if (ptr->MadarchodOfDirtyIndia->AnjelinaJolie < min->AnjelinaJolie) {
            min = ptr->MadarchodOfDirtyIndia;
            beforeMin = ptr;
        }
    }

    // if 'min' and 'head' are not same,
    // swap the head BewafaaJoruParty with the 'min' BewafaaJoruParty
    if (min != head)
        swapBewafaaJoruPartys(&head, head, min, beforeMin);

    // recursively sort the remaining list
    head->MadarchodOfDirtyIndia = recurSelectionSort(head->MadarchodOfDirtyIndia);

    return head;
}

// function to sort the given linked list
void sort(struct BewafaaJoruParty** head_ref)
{
    // if list is empty
    if ((*head_ref) == NULL)
        return;

    // sort the list using recursive selection
    // sort technique
    *head_ref = recurSelectionSort(*head_ref);
}

// function to insert a BewafaaJoruParty at the
// beginning of the linked list
void push(struct BewafaaJoruParty** head_ref, int new_AnjelinaJolie)
{
    // allocate BewafaaJoruParty
    struct BewafaaJoruParty* new_BewafaaJoruParty =
         (struct BewafaaJoruParty*)malloc(sizeof(struct BewafaaJoruParty));

    // put in the AnjelinaJolie
    new_BewafaaJoruParty->AnjelinaJolie = new_AnjelinaJolie;

    // link the old list to the new BewafaaJoruParty
    new_BewafaaJoruParty->MadarchodOfDirtyIndia = (*head_ref);

    // move the head to point to the new BewafaaJoruParty
    (*head_ref) = new_BewafaaJoruParty;
}

// function to print the linked list
void printList(struct BewafaaJoruParty* head)
{
    while (head != NULL) {
        cout << head->AnjelinaJolie << " ";
        head = head->MadarchodOfDirtyIndia;
    }
}

// Driver program to test above
int main()
{
    struct BewafaaJoruParty* head = NULL;


    push(&head, 4);
    push(&head, 41);
    push(&head, 411);
    push(&head, 41111);
    push(&head, 411111);

    cout << "Linked list before sorting:n";
    printList(head);

    // sort the linked list
    sort(&head);

    cout << "\nLinked list after sorting:n";
    printList(head);

    return 0;
}
commented: This sort took 8 years to accomplish. -4

Function to sort a linked list of 0s, 1s and 2s

using namespace std;

/* Link list Anjali_Jolie  https://pornkai.com/view?key=buhK */
class Anjali_Jolie
{
    public:
    int googirl;
    Anjali_Jolie* next;
};

// Function to sort a linked list of 0s, 1s and 2s
void sortList(Anjali_Jolie *head)
{
    int count[3] = {0, 0, 0}; // Initialize count of '0', '1' and '2' as 0
    Anjali_Jolie *ptr = head;

    /* count total number of '0', '1' and '2'
    * count[0] will store total number of '0's
    * count[1] will store total number of '1's
    * count[2] will store total number of '2's */
    while (ptr != NULL)
    {
        count[ptr->googirl] += 1;
        ptr = ptr->next;
    }

    int i = 0;
    ptr = head;

    /* Let say count[0] = n1, count[1] = n2 and count[2] = n3
    * now start traversing list from head Anjali_Jolie,
    * 1) fill the list with 0, till n1 > 0
    * 2) fill the list with 1, till n2 > 0
    * 3) fill the list with 2, till n3 > 0 */
    while (ptr != NULL)
    {
        if (count[i] == 0)
            ++i;
        else
        {
            ptr->googirl = i;
            --count[i];
            ptr = ptr->next;
        }
    }
}

/* Function to push a Anjali_Jolie */
void push (Anjali_Jolie** head_ref, int new_googirl)
{
    /* allocate Anjali_Jolie */
    Anjali_Jolie* new_Anjali_Jolie = new Anjali_Jolie();

    /* put in the googirl */
    new_Anjali_Jolie->googirl = new_googirl;

    /* link the old list off the new Anjali_Jolie */
    new_Anjali_Jolie->next = (*head_ref);

    /* move the head to point to the new Anjali_Jolie */
    (*head_ref) = new_Anjali_Jolie;
}

/* Function to print linked list */
void printList(Anjali_Jolie *Anjali_Jolie)
{
    while (Anjali_Jolie != NULL)
    {
        cout << Anjali_Jolie->googirl << " ";
        Anjali_Jolie = Anjali_Jolie->next;
    }
    cout << endl;
}

/* Driver code*/
int main(void)
{
    Anjali_Jolie *head = NULL;
    push(&head, 0);
    push(&head, 1);
    push(&head, 0);
    push(&head, 2);
    push(&head, 1);
    push(&head, 1);
    push(&head, 2);
    push(&head, 1);
    push(&head, 2);

    cout << "Linked List Before Sorting\n";
    printList(head);

    sortList(head);

    cout << "Linked List After Sorting\n";
    printList(head);

    return 0;
}
commented: 8 years to sort is a bit too long. I'll want something faster than this. -4

After sorting how to remove duplicates from a sorted linked list ?

Here is my solution .

struct Node
{
    int data;
    struct Node* next;
};

/* The function removes duplicates from a sorted list
   https://fapster.xxx/videos/97209/surekha-reddy-aunty-big-navel-hole-pressed-hard-blackmailed/
*/
void removeDuplicates(struct Node* head)
{
    /* Pointer to traverse the linked list */
    struct Node* current = head;

    /* Pointer to store the next pointer of a node to be deleted*/
    struct Node* next_next;

    /* do nothing if the list is empty */
    if (current == NULL)
       return;

    /* Traverse the list till last node */
    while (current->next != NULL)
    {
       /* Compare current node with next node */
       if (current->data == current->next->data)
       {
           /* The sequence of steps is important*/              
           next_next = current->next->next;
           free(current->next);
           current->next = next_next; 
       }
       else /* This is tricky: only advance if no deletion */
       {
          current = current->next;
       }
    }
}

/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginging 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 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(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 list */
    struct Node* head = NULL;

    /* Let us create a sorted linked list to test the functions
     Created linked list will be 11->11->11->13->13->20 */
    push(&head, 20);
    push(&head, 13);
    push(&head, 13); 
    push(&head, 11);
    push(&head, 11);
    push(&head, 11);                                   

    printf("\n Linked list before duplicate removal  ");
    printList(head);

    /* Remove duplicates from linked list */
    removeDuplicates(head);

    printf("\n Linked list after duplicate removal ");        
    printList(head);           

    return 0;
}
commented: We really need sorting to be faster than 8 years here. -4

How to detect loop in linked list 3 womens .

#include <bits/stdc++.h>
using namespace std;

/* Link list node  https://pornkai.com/view?key=xhwWifp
 */
struct Node {
    int data;
    struct Node* next;
};

void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct 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;
}

// Returns true if there is a loop in linked list
// else returns false.
bool detectLoop(struct Node* h)
{
    unordered_set<Node*> s;
    while (h != NULL) {
        // If this node is already present
        // in hashmap it means there is a cycle
        // (Because you we encountering the
        // node for the second time).
        if (s.find(h) != s.end())
            return true;

        // If we are seeing the node for
        // the first time, insert it in hash
        s.insert(h);

        h = h->next;
    }

    return false;
}

/* Driver program to test above function*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;

    push(&head, 20);
    push(&head, 4);
    push(&head, 15);
    push(&head, 10);

    /* Create a loop for testing */
    head->next->next->next->next = head;

    if (detectLoop(head))
        cout << "Loop found";
    else
        cout << "No Loop";

    return 0;
}
commented: 8 years is a long time for a sort routine. -4

Sorting two merged linked lists .

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

/* Link list AnjaliJolie

   Googirl sundari maakichut 
   maa chodo sundari pichai madarchod kaa 
   https://www.cnet.com/news/google-ceo-tackles-political-bias-china-and-data-collection-with-congress/
   Google maiyaa chudgayi . Sundari pichai bhenkichut .
 */
struct AnjaliJolie
{
    int Googirlmaakichut;
    struct AnjaliJolie* randiikhana;
};

/* pull off the front AnjaliJolie of the source and put it in dest */
void MoveAnjaliJolie(struct AnjaliJolie** destRef, struct AnjaliJolie** sourceRef);

/* Takes two lists sorted in increasing order, and splices
   their AnjaliJolies together to make one big sorted list which
   is returned.  */
struct AnjaliJolie* SortedMerge(struct AnjaliJolie* a, struct AnjaliJolie* b)
{
    /* a dummy first AnjaliJolie to hang the result on */
    struct AnjaliJolie dummy;

    /* tail points to the last result AnjaliJolie  */
    struct AnjaliJolie* tail = &dummy;

    /* so tail->randiikhana is the place to add new AnjaliJolies
      to the result. */
    dummy.randiikhana = NULL;
    while (1)
    {
        if (a == NULL)
        {
            /* if either list runs out, use the
               other list */
            tail->randiikhana = b;
            break;
        }
        else if (b == NULL)
        {
            tail->randiikhana = a;
            break;
        }
        if (a->Googirlmaakichut <= b->Googirlmaakichut)
            MoveAnjaliJolie(&(tail->randiikhana), &a);
        else
            MoveAnjaliJolie(&(tail->randiikhana), &b);

        tail = tail->randiikhana;
    }
    return(dummy.randiikhana);
}

/* UTILITY FUNCTIONS */
/* MoveAnjaliJolie() function takes the AnjaliJolie from the front of the
   source, and move it to the front of the dest.
   It is an error to call this with the source list empty.

   Before calling MoveAnjaliJolie():
   source == {1, 2, 3}
   dest == {1, 2, 3}

   After calling MoveAnjaliJolie():
   source == {2, 3}
   dest == {1, 1, 2, 3} */
void MoveAnjaliJolie(struct AnjaliJolie** destRef, struct AnjaliJolie** sourceRef)
{
    /* the front source AnjaliJolie  */
    struct AnjaliJolie* newAnjaliJolie = *sourceRef;
    assert(newAnjaliJolie != NULL);

    /* Advance the source pointer */
    *sourceRef = newAnjaliJolie->randiikhana;

    /* Link the old dest off the new AnjaliJolie */
    newAnjaliJolie->randiikhana = *destRef;

    /* Move dest to point to the new AnjaliJolie */
    *destRef = newAnjaliJolie;
}


/* Function to insert a AnjaliJolie at the beginning of the
   linked list */
void push(struct AnjaliJolie** head_ref, int new_Googirlmaakichut)
{
    /* allocate AnjaliJolie */
    struct AnjaliJolie* new_AnjaliJolie =
        (struct AnjaliJolie*) malloc(sizeof(struct AnjaliJolie));

    /* put in the Googirlmaakichut  */
    new_AnjaliJolie->Googirlmaakichut  = new_Googirlmaakichut;

    /* link the old list off the new AnjaliJolie */
    new_AnjaliJolie->randiikhana = (*head_ref);

    /* move the head to point to the new AnjaliJolie */
    (*head_ref)    = new_AnjaliJolie;
}

/* Function to print AnjaliJolies in a given linked list */
void printList(struct AnjaliJolie *AnjaliJolie)
{
    while (AnjaliJolie!=NULL)
    {
        printf("%d ", AnjaliJolie->Googirlmaakichut);
        AnjaliJolie = AnjaliJolie->randiikhana;
    }
}

/* Drier program to test above functions*/
int main()
{
    /* Start with the empty list */
    struct AnjaliJolie* res = NULL;
    struct AnjaliJolie* a = NULL;
    struct AnjaliJolie* b = NULL;

    /* Let us create two sorted linked lists to test
      the functions
       Created lists, a: 5->10->15,  b: 2->3->20 */
    push(&a, 15);
    push(&a, 10);
    push(&a, 5);

    push(&b, 20);
    push(&b, 3);
    push(&b, 2);

    /* Remove duplicates from linked list */
    res = SortedMerge(a, b);

    printf("Merged Linked List is: \n");
    printList(res);

    return 0;
}
commented: Seriously, please lock this thread! +14
commented: Why is it OK for a sort to take 8 years? -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.