Sorting a linked list

前端 未结 8 737
-上瘾入骨i
-上瘾入骨i 2020-11-29 09:26

I have written a basic linked list class in C#. It has a Node object, which (obviously) represents every node in the list.

The code does not use IEnumerable, however

8条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 10:23

    The simplest option is probably to extract the data, sort it in a mechanism that already supports sorting (arrays, List or IEnumerable via LINQ), and re-build the linked list with the sorted data.

    If you want to write your own sort algorithm, then you may find Comparer.Default useful (assuming you are using generics). This should allow you to compare any items that are IComparable or IComparable.

    As an aside - note that .NET already includes LinkedList etc; if this is just for learning etc, then fine ;-p

提交回复
热议问题