💥 C# | Runtime beats 98.00% [EXPLAINED]
Intuition\n\nWhen I first think about the problem of splitting a linked list into k parts, I imagine a line of people (which represents the linked list) standin
# Intuition\n\nWhen I first think about the problem of splitting a linked list into k parts, I imagine a line of people (which represents the linked list) standing in a queue. My job is to divide them into k groups. But here\'s the twist: I want the groups to be as equal in size as possible, and if they can\'t all be perfectly equal, the earlier groups should be slightly larger than the later ones.\n\nFor example, if there are 10 people and I need to make 3 groups, I\'d start by trying to give each group the same number of people. But 10 doesn\'t divide perfectly into 3, so two groups will have 4 people and one group will have 3 people. It\u2019s as if I\u2019m distributing "extra" people to the first few groups. I can apply this same thinking to the linked list, breaking it into chunks as equally as I can.\n\n# Approach\n\nCount nodes: First, I count how many nodes (n) are in the list.\n\nDivide parts: I determine the base size of each part by dividing n by k. The remainder (n % k) tells me how many parts get an extra node.\n\nSplit list: I move through the list, creating parts with the calculated size, and set the .next pointer to null to cut the list.\n\nEmpty parts: If there are more parts than nodes, the extra parts are null.\n\n# Complexity\n- Time complexity:\nO(n) because I traverse the list twice: once to count nodes and once to split it.\n\n- Space complexity:\nO(k) for storing the result array of k parts.\n\n# Code\n```csharp []\npublic class Solution {\n    public ListNode[] SplitListToParts(ListNode head, int k) {\n        // Step 1: Calculate the length of the linked list\n        int length = 0;\n        ListNode current = head;\n        while (current != null) {\n            length++;\n            current = current.next;\n        }\n\n        // Step 2: Determine the base size of each part and how many parts should be one node larger\n        int partSize = length / k;\n        int remainder = length % k;\n        \n        // Step 3: Create the result array and split the list\n        ListNode[] result = new ListNode[k];\n        current = head;\n\n        for (int i = 0; i < k; i++) {\n            result[i] = current;  // Start the i-th part\n            int currentPartSize = partSize + (i < remainder ? 1 : 0);  // Determine the size of the current part\n            \n            // Move the pointer to the end of the current part\n            for (int j = 0; j < currentPartSize - 1 && current != null; j++) {\n                current = current.next;\n            }\n            \n            // Break the link at the end of the current part\n            if (current != null) {\n                ListNode temp = current.next;\n                current.next = null;\n                current = temp;\n            }\n        }\n        \n        return result;\n    }\n}\n\n```
['C#']