C++ using two separate lists


#1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
ListNode* Solution::partition(ListNode* A, int B) {
    ListNode* less=new ListNode(0);
    ListNode* t1 =less;
    ListNode* greater= new ListNode(0);
    ListNode* t2 =greater;
    ListNode* temp=A;
    while(temp){
        if(temp->val <B){
            t1->next= new ListNode(temp->val);
            t1 =t1->next;
        }else{
            t2->next = new ListNode(temp->val);
            t2 =t2->next;
        }
        temp=temp->next;
    }
    if(less->next){
    t1 = less->next;
    while(t1->next){
        t1=t1->next;
    }
    if(greater->next)
        t1->next = greater->next;
    return less->next;
    }
    return greater->next;
}