关于leetcode个人解题总结:刷题17分隔链表

36次阅读

共计 414 个字符,预计需要花费 2 分钟才能阅读完成。

Leetcode:725. 分隔链表

class Solution {public ListNode[] splitListToParts(ListNode head, int k) {
        int len = 0;
        ListNode temp = head;
        while(temp != null){
            len++;
            temp = temp.next;
        }
        ListNode cur = head;
        int size = len / k;
        int mode = len % k;
        ListNode[] res = new ListNode[k];
        for(int i = 0;i < k;i++){res[i] = cur;
            int count = mode-- > 0 ? 1 : 0;
            for(int j = 0;j < size + count - 1;j++){if(cur != null)
                cur = cur.next;
            }
            if(cur != null){
                ListNode curTemp = cur.next;
                cur.next = null;
                cur = curTemp;
            }
        }
        return res;
    }
}

正文完
 0