86. Partition List

来源:互联网 发布:linux执行文件命令 编辑:程序博客网 时间:2024/06/14 11:50
class Solution {public:    ListNode* partition(ListNode* head, int x) {        if(!head||!head->next)            return head;        ListNode *less=NULL;        ListNode *more=NULL;        ListNode *morehead=NULL;        ListNode *lesshead=NULL;        while(head)        {            if(head->val<x)            {                if(!less) {                    less = head;                    lesshead=head;                    head=head->next;                }                else                {                    less->next=head;                    less=less->next;                    head=head->next;                }            }            else            {                if(!more)                {                    more=head;                    morehead=head;                    head=head->next;                }                else                {                    more->next=head;                    more=more->next;                    head=head->next;                }            }        }        if(!morehead)            return lesshead;        else if(!lesshead)            return morehead;        more->next=NULL;        less->next=morehead;        return lesshead;    }};
0 0
原创粉丝点击