public static <E extends Comparable<? super E>> Node<E> sort(Node<E> head) {
        Node<E> currentNode = head;
        Node<E> sortedHead = null;
        while (currentNode != null) {
            Node<E> toBeRemoved = currentNode;
            currentNode = currentNode.next;
            if (sortedHead == null) {
                // first element, copy it to output as it is
                sortedHead = toBeRemoved;
                sortedHead.next = null;
            } else {
                Node<E> sortedNode = sortedHead;
                Node<E> previous = null;
                while (sortedNode != null) {
                    if (sortedNode.data.compareTo(toBeRemoved.data) > 0) {
                        // found the place to insert the toBeRemoved node, If previous is null means current node will
                        // become new head of the sorted list. We break the loop once done with else code
                        if (previous == null) {
                            // toBeRemoved becomes new head of the sorted list
                            sortedHead = toBeRemoved;
                            sortedHead.next = sortedNode;
                        } else {
                            // toBeRemoved goes somewhere in between the list
                            previous.next = toBeRemoved;
                            toBeRemoved.next = sortedNode;
                        }
                        break;
                    }
                    previous = sortedNode;
                    sortedNode = sortedNode.next;
                    if (sortedNode == null) { // reached end of sorted list without inserting the toBeRemoved node
                        // toBeRemoved goes to end of sorted list
                        previous.next = toBeRemoved;
                        toBeRemoved.next = null;
                    }
                }
            }
        }
        return sortedHead;
    }