classSolution{publicListNode[]splitListToParts(ListNode head,int k){ListNode[] res =newListNode[k];ListNode cur = head;int len =0;// Obtain the lengthwhile(cur !=null){
cur = cur.next;++len;}int baseLen = len / k;// Min. element of a partint remainder = len % k;// Number of element should be put in the first part
cur = head;// Build all the partsfor(int i =0; i < k;++i){// Init. a part
res[i]= cur;// Build a partfor(int j =0; j < baseLen -1+(remainder >0?1:0);++j){if(cur ==null)break;
cur = cur.next;}// No two parts should have a size differing by more than one
remainder = remainder >0? remainder -1: remainder;// Break the tail, set cur for the next partif(cur !=null){ListNode next = cur.next;
cur.next =null;
cur = next;}}return res;}}