美文网首页
725. 分隔链表

725. 分隔链表

作者: 漫行者_ | 来源:发表于2021-09-24 00:04 被阅读0次

725. 分隔链表

初看题目的时候没有思路,第二天再看发现规律就还好,
注意余数和商就🆗了

class Solution {
    public ListNode[] splitListToParts(ListNode head, int k) {
        ListNode[] result = new ListNode[k];
        ListNode p = head;
        int length = 0;
        while(head != null) {
            length++;
            head = head.next;
        }
        int div = length/k;
        int yu = length%k;
        for(int i=0; i<yu; i++) {
            ListNode end = null;
            for(int j=0; j<div + 1; j++) {
                if(end == null) {
                    result[i] = p;
                } else {
                    end.next = p;
                }
                end = p;
                p = p.next;
            }
            end.next = null;
        }
        for(int i=yu; i<k; i++) {
            ListNode end = null;
            for(int j=0; j<div; j++) {
                if(end == null) {
                    result[i] = p;
                } else {
                    end.next = p;
                }
                end = p;
                p = p.next;
            }
            if(end != null) {
                end.next = null;
            }
        }
        return result;
    }
}

相关文章

  • 725. 分隔链表

    725. 分隔链表[https://leetcode-cn.com/problems/split-linked-l...

  • 4.链表(四)

    题目汇总https://leetcode-cn.com/tag/linked-list/725. 分隔链表中等(有...

  • LeetCode 725. 分隔链表

    725. 分隔链表 给定一个头结点为 root 的链表, 编写一个函数以将链表分隔为 k 个连续的部分。 每部分的...

  • Leetcode归类

    链表: Leetcode-725:分隔链表

  • 分隔链表

    给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。 你应当保留...

  • Swift - LeetCode - 分隔链表

    题目 分隔链表 问题: 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x ...

  • 86. 分隔链表

    86. 分隔链表 问题 给定一个链表和一个特定值 ,对链表进行分隔,使得所有小于 的节点都在大于或等于的节点之前。...

  • leetcode链表之分隔链表

    86、分隔链表[https://leetcode-cn.com/problems/partition-list/]...

  • leetcode链表之分隔链表

    725、分隔链表[https://leetcode-cn.com/problems/split-linked-li...

  • LeetCode 86. 分隔链表

    86. 分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点...

网友评论

      本文标题:725. 分隔链表

      本文链接:https://www.haomeiwen.com/subject/bsuagltx.html