C# Solution for Split Linked List In Parts Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe key intuition behind this solution is to split the list into k parts by distributin
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe key intuition behind this solution is to split the list into k parts by distributing the nodes as evenly as possible, ensuring that earlier parts get more nodes if there\u2019s a remainder. The main challenge is to maintain balance while splitting the list dynamically. The problem is approached in a way where the list is first traversed to determine its length, and then split by iterating through it again.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tTraverse to Determine the Length of the List:\n\t\u2022\tFirst, we traverse the entire list to calculate the number of nodes, length.\n2.\tCalculate the Size of Each Part:\n\t\u2022\tWe compute nodesPerPart = length / k, which gives the minimum number of nodes each part should have.\n\t\u2022\tThen, we compute remainder = length % k, which represents how many parts should get one extra node to distribute the remainder evenly.\n3.\tTraverse and Split the List:\n\t\u2022\tWe iterate over the list again and create k parts:\n    \t\u2022\tFor each part, if i < remainder, we assign nodesPerPart + 1 nodes to that part. Otherwise, we assign exactly nodesPerPart nodes.\n\t\u2022\tAfter assigning nodes for a part, we break the connection to the next part by setting the current node\u2019s next pointer to null, effectively splitting the linked list.\n4.\tHandle Null Parts:\n\t\u2022\tIf the number of parts (k) is greater than the length of the list, the remaining parts are set to null.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\u2022\tTraversing the List:\n\t\u2022\tWe traverse the entire linked list twice:\n\t    1.\tFirst pass: To calculate the length of the list, which takes O(n) time where n is the number of nodes.\n\t    2.\tSecond pass: To split the list into k parts, which also takes O(n) time.\n\t\u2022\tFinal Time Complexity: O(n) where n is the number of nodes in the linked list. This is because we make two linear passes through the list.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\u2022\tResult Array:\n\t\u2022\tWe create an array result of size k to store the head of each part, so the space complexity for this array is O(k).\n\u2022\tAuxiliary Space:\n\t\u2022\tApart from the array, we don\u2019t use any additional data structures that depend on the size of the input. We are modifying the input list in place by breaking connections, so no extra space is required for list copying.\n\u2022\tFinal Space Complexity: O(k) for the array that holds the head of each part.\n\n# Code\n```csharp []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     public int val;\n *     public ListNode next;\n *     public ListNode(int val=0, ListNode next=null) {\n *         this.val = val;\n *         this.next = next;\n *     }\n * }\n */\npublic class Solution {\n    public ListNode[] SplitListToParts(ListNode head, int k) {\n        ListNode[] result = new ListNode[k];\n        ListNode current = head;\n        \n        // Calculate length of list\n        int length = 0;\n        while (current != null) {\n            length++;\n            current = current.next;\n        }\n\n        // Calculate how many nodes per part\n        int nodesPerPart = length / k;\n        int remainder = length % k; // Extra nodes to distribute\n        \n        current = head;\n        \n        for (int i = 0; i < k && current != null; i++) {\n            result[i] = current;\n            int currentPartSize = nodesPerPart + (i < remainder ? 1 : 0); // Extra node to the first \'remainder\' parts\n\n            // Traverse to the end of this part\n            for (int j = 1; j < currentPartSize; j++) {\n                if (current != null) current = current.next;\n            }\n\n            // Disconnect current part from the rest of the list\n            if (current != null) {\n                ListNode nextPartHead = current.next;\n                current.next = null; // Split the list\n                current = nextPartHead; // Move to the next part\n            }\n        }\n\n        return result;\n    }\n}\n```
['C#']