题解 | #牛的品种排序IV#
牛的品种排序IV
https://www.nowcoder.com/practice/bd828af269cd493c86cc915389b02b9f
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*///black 0 white 1
ListNode* sortCowsIV(ListNode* head) {
// write code here
ListNode* wp = new ListNode(1);
ListNode* bp = new ListNode(0);
ListNode* black = bp;
ListNode* white = wp;
while(head)
{
//报错写法
// if(node->val==0)
// {
// black->val = 0;
// black = black->next;
// }
// if(node->val==1)
// {
// white->val = 1;
// white = white->next;
// }
if(head->val==0)
{
black->next = head;
black = black->next;
}
else
{
white->next = head;
white = white->next;
}
head = head->next;
}
white->next = NULL;
black->next = wp->next;
return bp->next;
}
};
SHEIN希音公司福利 350人发布