题解 | 牛的品种排序IV

牛的品种排序IV

https://www.nowcoder.com/practice/bd828af269cd493c86cc915389b02b9f

  1. 参考牛群分隔即可。

import java.util.*;

public class Solution {
    public ListNode sortCowsIV (ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode black = new ListNode(0);
        ListNode bTail = black;
        ListNode white = new ListNode(1);
        ListNode wTail = white;
        while (head != null) {
            if (head.val == 0) {
                bTail.next = head;
                bTail = bTail.next;
            } else {
                wTail.next = head;
                wTail = wTail.next;
            }
            head = head.next;
        }
        wTail.next = null;
        bTail.next = white.next;
        return black.next;
    }
}

全部评论

相关推荐

2025-12-11 14:24
门头沟学院 Java
牛客35720396...:不要用boss,全是骗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务