static LinkedListNode partition(LinkedListNode n, int p) {
        LinkedListNode beforeHead = null;
        LinkedListNode beforeTail = null;
        LinkedListNode afterHead = null;
        LinkedListNode afterTail = null;

        while (n != null) {
            if (n.getValue() < p) {
                if (beforeHead == null) {
                    beforeHead = n;
                    beforeTail = n;
                } else {
                    beforeTail.next = n;
                    beforeTail = beforeTail.next;
                }
            } else {
                if (afterHead == null) {
                    afterHead = n;
                    afterTail = n;
                } else {
                    afterTail.next = n;
                    afterTail = afterTail.next;
                }
            }
            n = n.next;
        }

        if (beforeTail != null) {
            beforeTail.next = afterHead;
            return beforeHead;
        }

        return afterHead;
    }