Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts.

The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.

The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.

Return an array of the k parts.

&nbsp;
Example 1:

Input: head = [1,2,3], k = 5
Output: [[1],[2],[3],[],[]]
Explanation:
The first element output[0] has output[0].val = 1, output[0].next = null.
The last element output[4] is null, but its string representation as a ListNode is [].


Example 2:

Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3
Output: [[1,2,3,4],[5,6,7],[8,9,10]]
Explanation:
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.


&nbsp;
Constraints:


	The number of nodes in the list is in the range [0, 1000].
	0 &lt;= Node.val &lt;= 1000
	1 &lt;= k &lt;= 50


# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
    def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
        length = 0
        cur = head
        while cur:
            length += 1
            cur = cur.next
        # DON'T do following since this makes head become null
        # while head:
        # length += 1
        # head = head.next

        # calculate the base size and the number of parts contain extra number
        size, extra = length // k, length % k

        # create empty list to store split parts
        res = [[] for _ in range(k)]

        # use two ptrs to split parts
        prev, cur = None, head

        for i in range(k):
            res[i] = cur
            # if this part contains extra number, it has (size+1) number
            for j in range(size + (1 if extra > 0 else 0)):
                prev, cur = cur, cur.next
            if prev: prev.next = None
            extra -= 1

        return res
class Solution {
    public ListNode[] splitListToParts(ListNode head, int k) {
        ListNode[] arr=new ListNode[k];

        if(k<2 || head==null || head.next==null){
            arr[0]=head;
            return arr;
        }

        ListNode temp=head;
        int len=1;
        while(temp.next!=null){
            len++;
            temp=temp.next;
        }

        int partition= len/k; //no of part 3
        int extra=len%k; //extra node 1 0

        ListNode curr=head;
        ListNode prev=null;
        int index=0;
        while(head!=null){
            arr[index++]=curr;
            for(int i=0; i<partition && curr!=null ; i++){
                prev=curr;
                curr=curr.next;
            }
            if(extra>0){
                prev=curr;
                curr=curr.next;
                extra--;
            }
            head=curr;
            prev.next=null;

        }
     return arr;
    }
}
class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* head, int k)
    {
       vector<ListNode*>ans;
       int len=0;
        ListNode*temp=head;
       while(temp!=NULL)
           len++,temp=temp->next;

       int y=len/k , z=len%k;

       while(head !=NULL)
       {
           ans.push_back(head);
           int count=1;
           while(head!=NULL && count<y)
                 head=head->next,count++;

           if(z && y)
           {
               head=head->next;z--;
           }
           if(head==NULL) continue;
           ListNode*temp=head->next;
           head->next=NULL;
           head=temp;
       }
       while(ans.size()<k)
       {
           ans.push_back(NULL);
       }

       return ans;
    }
};
var splitListToParts = function(head, k) {
    let length = 0, current = head, parts = [];

    while (current) {
        length++;
        current = current.next;
    }

    let base_size = Math.floor(length / k), extra = length % k;
    current = head;

    for (let i = 0; i < k; i++) {
        let part_size = base_size + (extra > 0 ? 1 : 0);
        let part_head = null, part_tail = null;

        for (let j = 0; j < part_size; j++) {
            if (!part_head) {
                part_head = part_tail = current;
            } else {
                part_tail.next = current;
                part_tail = part_tail.next;
            }

            if (current) {
                current = current.next;
            }
        }

        if (part_tail) {
            part_tail.next = null;
        }

        parts.push(part_head);
        extra = Math.max(extra - 1, 0);
    }

    return parts;
};
Split Linked List in Parts