private void nextPermutation(Node list, Node permutationStart, Node permutationEnd, PermutationHandler handler) {
        if (list == null) {
            buildPermutation(permutationStart, handler);
            return;
        }

        Node current = list;
        Node previous = null;
        Node next = null;
        Node sublist = list;
        while (current != null) {
            sublist = list == current ? current.next : list;
            previous = current.previous;
            next = current.next;

            if (previous != null) {
                previous.next = next;
            }
            if (next != null) {
                next.previous = previous;
            }
            current.previous = permutationEnd;
            current.next = null;
            permutationEnd.next = current;

            nextPermutation(sublist, permutationStart, current, handler);

            if (previous != null) {
                previous.next = current;
            }
            if (next != null) {
                next.previous = current;
            }
            current.previous = previous;
            current.next = next;

            current = current.next;
        }
    }