public static void Main()
Node<int> head = FormLinkedListNodesForInsertionSort();
Print(head, "Original List before Sorting: ");
Console.WriteLine("");Console.WriteLine("");
Node<int> sorted = InsertionSort(head);
Print(sorted, "After Sorting: ");
private static void Print(Node<int> node, string str)
Console.Write(node.data + " --> ");
private static Node<int> FormLinkedListNodesForInsertionSort()
Node<int> node = new Node<int>();
node.next = new Node<int>();
node.next.next = new Node<int>();
node.next.next.next = new Node<int>();
node.next.next.next.data = 1;
node.next.next.next.next = new Node<int>();
node.next.next.next.next.data = 8;
node.next.next.next.next.next = new Node<int>();
node.next.next.next.next.next.data = 7;
node.next.next.next.next.next.next = new Node<int>();
node.next.next.next.next.next.next.data = 2;
node.next.next.next.next.next.next.next = new Node<int>();
node.next.next.next.next.next.next.next.data = 4;
node.next.next.next.next.next.next.next.next = null;
private static Node<int> InsertionSort(Node<int> head)
Node<int> current = head;
Node<int> next = current.next;
sorted = SortNodes(current, sorted);
private static Node<int> SortNodes(Node<int> newnode, Node<int> sorted)
if (sorted == null || sorted.data >= newnode.data)
Node<int> current = sorted;
while (current.next != null && current.next.data < newnode.data)
newnode.next = current.next;