题目描述:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};*/
#include<malloc.h>
#define NULL __null
class Partition {
public:
ListNode* partition(ListNode* pHead, int x) {
ListNode* lHead, * gHead ;
ListNode* llist, * glist ;
lHead = llist = (ListNode* )malloc(sizeof(ListNode));
gHead = glist = (ListNode*)malloc(sizeof(ListNode));
ListNode* Head = pHead;
llist->next=glist->next=NULL;
while (Head)
{
if (Head->val < x)
{
llist->next = Head;
llist = llist->next;
Head = Head->next;
}
else
{
glist->next = Head;
glist = glist->next;
Head = Head->next;
}
}
llist->next = gHead->next;
pHead = lHead->next;
glist->next = NULL;
free(lHead);
free(gHead);
lHead = gHead = NULL;
return pHead;
}
};