private Node _head = null;
public LinkedList(int[] inputData)
foreach (int data in inputData)
public void PrintAllNodes(string inputText = null)
if (!String.IsNullOrEmpty(inputText))
Console.WriteLine(inputText);
Console.Write(current.data + " ");
if (!String.IsNullOrEmpty(inputText))
Console.WriteLine("-------------------");
public void AddLast(int data)
while (current.next != null)
public void AddLast(Node node)
while (current.next != null)
if (current.next != null)
var headToReturn = _head;
public static LinkedList[] GetInitialList()
new LinkedList(new int[]{8, 9}),
new LinkedList(new int[]{1, 2, 3}),
new LinkedList(new int[]{4, 5, 6}),
new LinkedList(new int[]{7, 10})
private static void performHeapSort(LinkedList[] arr)
for (int i = n / 2 - 1; i >= 0; i--)
for (int i = n - 1; i >= 0; i--)
LinkedList temp = arr[0];
private static void rebuildHeap(LinkedList[] arr, int n, int i)
if (l < n && arr[l].GetHead().data > arr[largest].GetHead().data)
if (r < n && arr[r].GetHead().data > arr[largest].GetHead().data)
LinkedList swap = arr[i];
rebuildHeap(arr, n, largest);
public static void PrintListArray(LinkedList[] listArray)
Console.WriteLine("List Array:");
foreach (LinkedList list in listArray)
Console.WriteLine("----");
public static void Main()
var initialList = GetInitialList();
var resultList = new LinkedList();
PrintListArray(initialList);
while (initialList.Length > 0)
performHeapSort(initialList);
var newNode = initialList[0].RemoveHead();
resultList.AddLast(newNode);
PrintListArray(initialList);
resultList.PrintAllNodes("Result list:");
if (!initialList[0].HasNodes())
initialList = initialList.Where((val, i) => i != 0).ToArray();