Easy Solution | Java | O(n) Time | O(1) space


#1

Use 2 pointers to seperate out less values than B and greater values than B
Merge them together…when we reach the end.

public ListNode partition(ListNode A, int B) {
        if(A == null)
            return A;
            
        ListNode lessThanB = new ListNode(0), lItr = lessThanB;
        ListNode greaterThanB = new ListNode(0), gItr = greaterThanB;
        
        while(A != null) {
            if(A.val >= B) {
                gItr.next = A;
                gItr = gItr.next;
            }
            else {
                lItr.next = A;
                lItr = lItr.next;
            }
            A = A.next;
        }
        lItr.next = greaterThanB.next;
        gItr.next = null;
        
        return lessThanB.next;
        
        
    }