/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param x int整型
* @return ListNode类
*/
ListNode* cow_partition(ListNode* head, int x) {
// write code here
ListNode* temp1 = new ListNode(0);
ListNode* head1 = temp1;
ListNode* temp3 = new ListNode(0);
ListNode* head3 = temp3;
if (head == NULL || head->next == NULL) return head;
while (head) {
if (head->val < x) {
ListNode* temp2 = new ListNode(0);//创建一个接受小于x的值的链表
temp2->val = head->val;
temp1->next = temp2;
temp1 = temp2;
} else {
ListNode* temp2 = new ListNode(0);//接收大于等于x的值的链表
temp2->val = head->val;
temp3->next = temp2;
temp3 = temp2;
}
head = head->next;
}
temp1->next=head3->next;//合并链表
head3->next==NULL;
return head1->next;
}
};