Linked List sorting


what is the code to sort a Linked List of Linked lists ? thank you for your help.
Hi there,

That depends on several factors, such as which sorting algorithm you would like to use and what you would like to sort on.

Are the contained linked list sorted themselves, or do they need to be sorted too?
Are you using std::list or a custom implementation?

If you would be so kind as to provide us some more information I'm someone will be happy to help you.

All the best,
Thank you so much! the contained linked lists (of char *) are already sorted, I'm not sure which sorting algorithm I have to use, it doesn't matter I guess.
I have to sort the "big" linked list alphabetically according to the first element in each contained list.

hope I'm clear enough

thank you again

for example

car rac
dog god

will become :

car rac
dog god

(actually this is a final step of a big problem about finding anagrams and sorting them, I did all the work this part is still missing)
Last edited on
Hi there,

Is your "small list" a different list-type (class) than your "big list"?
If so, you could simply overload operator<() and use std::sort to do the hard work:

If you are required to do it "manually", for linked lists i think bubble sort is probably the easiest to implement because it just traverses the list. A very good explanation on bubble sort is available here:

In short:

- Loop through the big list, if the first element of the child is smaller than the next one, swap the two.
- keep doing this until the list is sorted (no swaps were made).

Please do come back to us with some of your code if you require any further assistance.

All the best,

#include <iostream>
#include <algorithm>
#include <string>
#include <llist.h>
#include <fstream>

using namespace std;

template <typename E> class sortedLList: public LList<E> {

sortedLList () : LList () {};

void removeall() { // Return link nodes to free store
while(head != NULL) {
curr = head;
head = head->next;
delete curr;

~sortedLList() { removeall(); }

void insert (const E& item)

curr = head;

if ( lessthan(item,curr->next->element) ){break;}
if(curr->next == tail) {curr = curr->next; break;}
curr = curr->next;

curr->next = new Link<E>(item, curr->next);
if (tail == curr) tail = curr->next; // New tail


void print()
Link<E> * g = curr;

curr = head->next;
cout<<curr->element<<' ';
curr = curr->next;

curr = g;



bool findAnagram (char * t, char *s)
{ string a=t;
string b=s;
char *y;
char *x;
y=new char [a.size ()+1];
x=new char [b.size()+1];
std::sort (a.begin(),a.end());
std::sort (b.begin (), b.end ());
std::strcpy (y,a.c_str());

if (strcmp (x,y)==0) return true;
if (strcmp(x,y)!=0) return false;


bool lessthan(char * a, char * b);

int main ()

ifstream in;
char *t1;
char *t2;
char *t3;
string s2;
char * t;
string s; ("anagramEasy.txt",ios::in);
sortedLList<char *> A;
LList <sortedLList<char *> *> B;

B.insert (&A);

t1=new char [s.size ()+1];

while (!
t3=new char [s.size()+1];

std::strcpy (t3,s.c_str());

std::sort (s.begin (), s.end ());
t2=new char [s.size()+1];
std::strcpy (t2,s.c_str());
bool i=false;
while (B.curr->next!=NULL)
{ s2=B.getValue ()->head->next->element;

std::sort (s2.begin (), s2.end ());
t=new char [s2.size()+1];
std::strcpy (t,s2.c_str());
if (findAnagram (t,t2))

if (i==false )
sortedLList<char *>*C= new sortedLList <char *> ;
C->insert (t3);



int r;

char* temp;
char* m;
temp=new char [];
m=new char [];
n=new char [];

for (B.curr=B.head;B.curr->next->next!=NULL;B.curr=B.curr->next)


if (n<m)


int n=B.length();


for (B.curr=B.head;B.curr->next!=NULL;B.curr=B.curr->next)
B.getValue ()->print();
cout << endl;

bool lessthan(char * a, char * b)

string s1 = a;
string s2 = b;
if (s1<s2) return true; else return false;


this is my code and i'd like to sort B
Given you have a linked list of linked lists, you first need to lexicographically sort the strings in the linked lists, and then lexicographically sort the lists in the linked list based on their first element.
that's exactly what I don't how to do, how can I " lexicographically sort the lists in the linked list based on their first element. " ?
Suppose that I have a list of lists L, and that the elements of each L[i] are sorted. Then, to sort L, I would iterate it, comparing against L[i][0]. Does that make more sense?

Here is some pseudo code:

    has_swapped = false
    for i from 0 to L.size - 1,
        if L[i][0] > L[i + 1][0]
            swap(L[i][0], L[i + 1][0])
            has_swapped = true
while has_swapped
Last edited on
how can I transform a List into an array?
You don't have to transform the list into an array; I used array notation only to be succinct.

Here's a visualization of your list:

The red circles are what you're comparing against to sort L.

I hope this vivifies my suggestion above.

Edit: here's a quick, commented, working example of everything I've been suggesting to you:

If I were you, I would add a (re) sort method to my linked list class that takes a comparer lambda expression; this would make it even more general, as I could then kill many birds with one stone.
Last edited on
Topic archived. No new replies allowed.