Easy Java Solution


  • 1
    K
    class Solution {
        public ListNode[] splitListToParts(ListNode root, int k) {
            int m=0;
            ListNode temp=root;
            while(root!=null){
                root=root.next;
            m++;
            }
            ListNode[] res= new ListNode[k];
            int size=m/k;
            int l= m%k;
            
            for(int i=0;i<k;i++){
                ListNode ans= temp;
                int j=size;
                while(temp!=null && j>1){
                    temp=temp.next;j--;
                }
                if(size!=0 && l>0 && temp!=null){ temp=temp.next;  l--;}
                ListNode temp1=temp;
                if(temp!=null) temp=temp.next;
                if(temp1!=null) temp1.next=null;
                res[i]=ans;
            }
            return res;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.