Video Solution | Explanation With Drawings | In Depth | Java | C++
Intuition, approach and complexity discussed in detail in video solution\nhttps://youtu.be/NhJlizFZyc0\n\n# Code\nC++\n\nclass Solution {\npublic:\n    vector<L
# Intuition, approach and complexity discussed in detail in video solution\nhttps://youtu.be/NhJlizFZyc0\n\n# Code\nC++\n```\nclass Solution {\npublic:\n    vector<ListNode*> splitListToParts(ListNode* head, int k) {\n        int len = 0; \n        for(ListNode *curr = head; curr != NULL; curr = curr->next){\n            len++;\n        }\n        vector<ListNode*> result(k);\n        int extras = len % k, equals = len / k, indx = 0, size = 0;\n        ListNode *currHead = head, *currTail = NULL, *curr = head;\n        for(; indx < extras; indx++){\n              while(curr != NULL && size < equals + 1){\n                  if(currHead == NULL)currHead = curr;\n                  currTail = curr;\n                  curr = curr->next;\n                  size++;\n              }\n              result[indx] = (size > 0) ? currHead : NULL;\n              if(currTail != NULL){\n                  ListNode *next = currTail->next;\n                  currTail->next = NULL;\n                  curr = next;\n                   size = 0; \n              }\n              currHead = NULL;\n        }\n        for(indx = extras; indx < k; indx++){\n             while(curr != NULL && size < equals){\n                  if(currHead == NULL)currHead = curr;\n                  currTail = curr;\n                  curr = curr->next;\n                  size++;\n              }\n\n              result[indx] = (size > 0) ? currHead : NULL;\n              if(currTail != NULL){\n                  ListNode *next = currTail->next;\n                  currTail->next = NULL;\n                  curr = next;\n                  size = 0;\n              }\n              currHead = NULL;\n        }\n        return result;\n    }\n};\n```\nJava\n```\nclass Solution {\n    public ListNode[] splitListToParts(ListNode head, int k) {\n        int len = 0; \n        for(ListNode curr = head; curr != null; curr = curr.next){\n            len++;\n        }\n        ListNode result[] = new ListNode[k];\n        int extras = len % k, equals = len / k, indx = 0, size = 0;\n        ListNode currHead = head, currTail = null, curr = head;\n        for(; indx < extras; indx++){\n              while(curr != null && size < equals + 1){\n                  if(currHead == null)currHead = curr;\n                  currTail = curr;\n                  curr = curr.next;\n                  size++;\n              }\n              result[indx] = (size > 0) ? currHead : null;\n              if(currTail != null){\n                  ListNode next = currTail.next;\n                  currTail.next = null;\n                  curr = next;\n                   size = 0; \n              }\n              currHead = null;\n        }\n        for(indx = extras; indx < k; indx++){\n             while(curr != null && size < equals){\n                  if(currHead == null)currHead = curr;\n                  currTail = curr;\n                  curr = curr.next;\n                  size++;\n              }\n              result[indx] = (size > 0) ? currHead : null;\n              if(currTail != null){\n                  ListNode next = currTail.next;\n                  currTail.next = null;\n                  curr = next;\n                  size = 0;\n              }\n              currHead = null;\n        }\n        return result;\n    }\n}\n```
['C++', 'Java']