//现有一链表的头指针 ListNode* pHead,给一定值x,//编写一段代码将所有小于x的集中于前半部分,大于x的集中于后半部分,//若链表内有x则居于中间,没有则不显示//且不能改变原来的数据相对顺序,返回重新排列后的链表的头指针。//示例 x=5//1 6 2 7 3 5 9 8 (5)-->1 2 3 5 6 7 9 8//1 6 2 7 8 5 9 3 (5)-->1 2 3 5 6 7 8 9 //1 2 3 (5)-->1 2 3//6 7 8 (5)--->6 7 8//5 5 5 (5)--->5 5 5 ListNode* Partition(ListNode* phead,int x){ ListNode* smallhead = NULL; ListNode* smallx = NULL; ListNode* bighead = NULL; ListNode* bigx = NULL; ListNode* flaghead = NULL; ListNode* flagx = NULL; if (phead == NULL) return NULL; while (phead) { if (phead->val < x) { if (smallhead == NULL) { smallx = phead; smallhead = smallx; } else { smallx->next = phead; smallx = smallx->next; } } else if (phead->val > x) { if (bighead == NULL) { bigx = phead; bighead = bigx; } else { bigx->next = phead; bigx = bigx->next; } } else { if (flaghead == NULL) { flagx = phead; flaghead = flagx; } else { flagx->next = phead; flagx = flagx->next; } } phead = phead->next; } if(bigx) bigx->next = NULL; if (flaghead != NULL) { if (smallhead == NULL) { flagx->next = bighead; return flaghead; } else { smallx->next = flaghead; flaghead->next = bighead; return smallhead; } } else { if (smallhead == NULL) { return bighead; } else { smallx->next = bighead; return smallhead; } }}