sort linked list using BST

This is a discussion on sort linked list using BST within the C Programming forums, part of the General Programming Boards category; Hi, in FAQ, Prelude's corner in section Linked List I found this:
". Another alternative way of sorting a linked ...

sort linked list using BST

Hi, in FAQ, Prelude's corner in section Linked List I found this:
". Another alternative way of sorting a linked list is not obvious at first, but uses the fact that a binary tree is always sorted. Simply walk the list, removing the first item and inserting it into a binary tree. Then do an inorder traversal of the tree, inserting at the end of a new list. Often it makes sense to switch between data structures, despite what people may tell you. "

And, of coure it's not working properly. I think that Binary Tree is created ok, but problem is how to create new list from tree in HelperSort because head is created again and again in recursion. I was thinking to add another argument to function, int value to ensure head is created only once. But there must be another way.

Oh no, I can't believe it, I can't believe it, I refuse to believe it. I can swear I tried that before I posted this issue. I don't know how this happened.
Thanks Prelude, I knew it is only a matter of minutes when you'll respond. I tried to implement your code and actually I manage to get it works:

>I think it would be good to destroy tree after I've done with it.
Um, that wouldn't be wise. The whole reason that this algorithm is reasonably efficient is because malloc isn't called. The tree is built using nodes already in the list and the list is built using nodes from the tree. The only thing we're doing is performing link surgery. That's why the list must be doubly linked, because the left and right tree links correspond to forward and back list links.

Anyway, since the nodes already exist and are just being restructured, the result of destroying the tree would be destroying the list as well. While that certainly makes future searching and sorting operations zippy, it's not conducive to a robust program.

>However you mentioned earlier that insert sort is very useful with sorting linked list.
Mergesort is better for large lists, insertion sort is quick and easy for short lists.

>What I did was to create BST from linked list, and then create a complete new linked list.
There's no reason to do this because not only will you be creating new nodes, you will also need to destroy the old nodes to avoid memory leaks. By reusing back and forward links as left and right links, you can avoid a lot of overhead. The key is noticing that it doesn't really matter what the links do as long as both data structures use the same number of them.

>I kwas wondering about efficiency.
See if you can think of any ways to do this without an explicit binary search tree so that an efficient array based sort is possible.

I would also like if you can examine parts of this code, especially functions RemoveEnd nad RemoveHead. I think that is correct, but for me it looks like a little bulky. If you can do that and give your opinion I would greatly appreciate it.
Thanks!