【leetcode】86. 分隔链表

 

struct ListNode* partition(struct ListNode* head, int x){
    struct ListNode* left=(struct ListNode*)calloc(sizeof(struct ListNode),1);
    struct ListNode* right=(struct ListNode*)calloc(sizeof(struct ListNode),1);
    struct ListNode* l=left, *r=right;
    while(head){
        if(head->val < x){
            l->next=head;
            l=head;
        }
        else{
            r->next=head;
            r=head;
        }
        head=head->next;
    }
    r->next=NULL;
    l->next=right->next;
    return left->next;
}

 

posted @ 2020-12-13 17:45  温暖了寂寞  阅读(83)  评论(0编辑  收藏  举报