/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (head == NULL)
return NULL;
ListNode *pCur;
ListNode *pFront;
ListNode *pRear;
ListNode *pNewHead = NULL;
ListNode *pRearHead = NULL;
pCur = head;
pFront = NULL;
pRear = NULL;
while (pCur != NULL)
{
if (pCur->val < x)
{
if (pFront == NULL)
{
pFront = pCur;
pNewHead = pCur;
}
else
{
pFront->next = pCur;
pFront = pFront->next;
}
}
else
{
if (pRear == NULL)
{
pRear = pCur;
pRearHead = pCur;
}
else
{
pRear->next = pCur;
pRear = pRear->next;
}
}
pCur = pCur->next;
}
if (pFront)
{
pFront->next = pRearHead;
if (pRear)
pRear->next = NULL;
}
else if (pRearHead)
{
pNewHead = pRearHead;
}
return pNewHead;
}
};
leetcode: Partition List
最新推荐文章于 2018-10-12 09:53:13 发布