/** * 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; } };