✅95%||✅Full Explanation🔥||Beginner Friendly🔥||Two-Pass Method✅
Problem Understanding\nThe problem asks us to split a singly linked list into k consecutive linked list parts while maintaining two important conditions:\n\n1. 
# Problem Understanding\nThe problem asks us to **split a singly linked list into k consecutive** linked list parts while maintaining two important conditions:\n\n1. The length of each part should be **as equal as possible**.\n2. **No two parts should have a size differing by more than one**.\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe key insight here is that we can efficiently split the linked list into **equal-sized parts** by calculating the **minimum guaranteed size** of each part (n) and distributing any extra nodes (r) as evenly as possible among the first r parts. **This approach ensures that we meet both conditions specified in the problem statement**.\n\n**By maintaining two pointers (node and prev)** and updating them as we traverse the linked list, we can keep track of the current part and efficiently split the list into k parts.\n\n# Approach\n**To solve this problem efficiently, we can follow these steps:**\n<!-- Describe your approach to solving the problem. -->\n1. **Calculate the Length:** First, we need to **determine the length of the linked list**. This is **important because it helps** us evenly distribute the nodes among the k parts.\n```\nfor (ListNode* node = root; node; node = node->next)\n            len++;\n```\n2. **Calculate Sizes:** Calculate the minimum guaranteed size of each part, n, **by dividing the total length by k**. **Also, calculate the number of extra nodes, r**, which will be spread among the first r parts (remainder of length / k).\n```\n // Calculate the minimum guaranteed part size (n) and the number of extra nodes (r).\n        int n = len / k, r = len % k;\n```\n3. **Traverse and Split:** Initialize **two pointers**, node and prev, **to traverse the linked list.** Then, loop through each part:\n    - **Store the current node** as the start of the current part in the parts array.\n    - **Traverse n + 1 nodes** if there are remaining extra nodes (when r > 0). Otherwise, traverse only n nodes.\n    - After traversing, **we disconnect the current part from the rest of the list by setting prev->next to nullptr**. This ensures that each part is isolated.\n```\n// Loop through each part.\n        for (int i = 0; node && i < k; i++, r--) {\n            // Store the current node as the start of the current part.\n            parts[i] = node;\n            // Traverse n + 1 nodes if there are remaining extra nodes (r > 0).\n            // Otherwise, traverse only n nodes.\n            for (int j = 0; j < n + (r > 0); j++) {\n                prev = node;\n                node = node->next;\n            }\n            // Disconnect the current part from the rest of the list by setting prev->next to nullptr.\n            prev->next = nullptr;\n        }\n```   \n4. **Return Result:** Return the parts array containing the k parts of the linked list.\n\n---\n\n\n# Till now if any doubt, Please see step by step Guide:\n1. **We start by creating an array parts of size k** to store the resulting linked list parts. Initially, all elements are set to nullptr.\n2. **We calculate the length of the input linked list by traversing it using a for loop**. This helps us determine the size of each part.\n3. **We calculate two important values:**\n    - **n: The minimum guaranteed size of each part**, obtained by dividing the total length by k.\n    - **r: The number of extra nodes that need to be distributed among the first r parts**, where r is the remainder of the division.\n4. **We initialize two pointers, node and prev**, to traverse the linked list. **node starts at the head of the linked list** (root), and **prev keeps track of the previous node.**\n5. We loop through **each part of the result:**\n\n    - Store the current node **as the start of the current part in the parts array.**\n    - **Traverse n + 1 nodes if there are remaining extra nodes** (when r > 0). Otherwise, traverse only n nodes.\n    - **After traversing, we disconnect the current part** from the rest of the list by setting prev->next to nullptr. This ensures that each part is isolated.\n6. **Finally, we return the parts array** containing the k parts of the linked list.\n# Complexity\n- Time complexity: O(N), where N is the number of nodes in the linked list.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(K), where k is the number of parts in which the linked list is split.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n\n---\n\n# SMALL REQUEST : If you found this post even remotely helpful, be kind enough to smash a upvote. I will be grateful.I will be motivated\uD83D\uDE0A\uD83D\uDE0A\n\n# Code\n```C++ []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    vector<ListNode*> splitListToParts(ListNode* root, int k) {\n        // Create a vector of ListNode pointers to store the k parts.\n        vector<ListNode*> parts(k, nullptr);\n\n        // Calculate the length of the linked list.\n        int len = 0;\n        for (ListNode* node = root; node; node = node->next)\n            len++;\n\n        // Calculate the minimum guaranteed part size (n) and the number of extra nodes (r).\n        int n = len / k, r = len % k;\n\n        // Initialize pointers to traverse the linked list.\n        ListNode* node = root, *prev = nullptr;\n\n        // Loop through each part.\n        for (int i = 0; node && i < k; i++, r--) {\n            // Store the current node as the start of the current part.\n            parts[i] = node;\n            // Traverse n + 1 nodes if there are remaining extra nodes (r > 0).\n            // Otherwise, traverse only n nodes.\n            for (int j = 0; j < n + (r > 0); j++) {\n                prev = node;\n                node = node->next;\n            }\n            // Disconnect the current part from the rest of the list by setting prev->next to nullptr.\n            prev->next = nullptr;\n        }\n\n        // Return the array of k parts.\n        return parts;\n    }\n};\n```\n```Java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode[] splitListToParts(ListNode root, int k) {\n        // Create an array of ListNode pointers to store the k parts.\n        ListNode[] parts = new ListNode[k];\n\n        // Calculate the length of the linked list.\n        int len = 0;\n        ListNode node = root;\n        while (node != null) {\n            len++;\n            node = node.next;\n        }\n\n        // Calculate the minimum guaranteed part size (n) and the number of extra nodes (r).\n        int n = len / k, r = len % k;\n\n        // Reset the pointer to the beginning of the linked list.\n        node = root;\n        ListNode prev = null;\n\n        // Loop through each part.\n        for (int i = 0; node != null && i < k; i++, r--) {\n            // Store the current node as the start of the current part.\n            parts[i] = node;\n\n            // Traverse n + 1 nodes if there are remaining extra nodes (r > 0).\n            // Otherwise, traverse only n nodes.\n            for (int j = 0; j < n + (r > 0 ? 1 : 0); j++) {\n                prev = node;\n                node = node.next;\n            }\n\n            // Disconnect the current part from the rest of the list by setting prev.next to null.\n            if (prev != null) {\n                prev.next = null;\n            }\n        }\n\n        // Return the array of k parts.\n        return parts;\n    }\n}\n\n```\n```Python3 []\n# Definition for singly-linked list.\nclass ListNode:\n    def __init__(self, val=0, next=None):\n        self.val = val\n        self.next = next\n\nclass Solution:\n    def splitListToParts(self, root, k):\n        # Create a list of ListNode pointers to store the k parts.\n        parts = [None] * k\n\n        # Calculate the length of the linked list.\n        len = 0\n        node = root\n        while node:\n            len += 1\n            node = node.next\n\n        # Calculate the minimum guaranteed part size (n) and the number of extra nodes (r).\n        n, r = divmod(len, k)\n\n        # Reset the pointer to the beginning of the linked list.\n        node = root\n        prev = None\n\n        # Loop through each part.\n        for i in range(k):\n            # Store the current node as the start of the current part.\n            parts[i] = node\n\n            # Traverse n + 1 nodes if there are remaining extra nodes (r > 0).\n            # Otherwise, traverse only n nodes.\n            for j in range(n + (1 if r > 0 else 0)):\n                prev = node\n                node = node.next\n\n            # Disconnect the current part from the rest of the list by setting prev.next to None.\n            if prev:\n                prev.next = None\n\n            # Decrement the count of extra nodes (r) if applicable.\n            if r > 0:\n                r -= 1\n\n        # Return the list of k parts.\n        return parts\n\n```\n``` Javascript []\n// Definition for singly-linked list.\nfunction ListNode(val, next) {\n    this.val = val;\n    this.next = next || null;\n}\n\nvar splitListToParts = function(root, k) {\n    // Create an array of ListNode pointers to store the k parts.\n    const parts = new Array(k).fill(null);\n\n    // Calculate the length of the linked list.\n    let len = 0;\n    let node = root;\n    while (node) {\n        len++;\n        node = node.next;\n    }\n\n    // Calculate the minimum guaranteed part size (n) and the number of extra nodes (r).\n    const n = Math.floor(len / k);\n    let r = len % k;\n\n    // Reset the pointer to the beginning of the linked list.\n    node = root;\n    let prev = null;\n\n    // Loop through each part.\n    for (let i = 0; node && i < k; i++, r--) {\n        // Store the current node as the start of the current part.\n        parts[i] = node;\n\n        // Traverse n + 1 nodes if there are remaining extra nodes (r > 0).\n        // Otherwise, traverse only n nodes.\n        for (let j = 0; j < n + (r > 0 ? 1 : 0); j++) {\n            prev = node;\n            node = node.next;\n        }\n\n        // Disconnect the current part from the rest of the list by setting prev.next to null.\n        if (prev) {\n            prev.next = null;\n        }\n    }\n\n    // Return the array of k parts.\n    return parts;\n};\n\n```\n\n---\n\n\n# SMALL REQUEST : If you found this post even remotely helpful, be kind enough to smash a upvote. I will be grateful.I will be motivated\uD83D\uDE0A\uD83D\uDE0A\n\n![UPVOTE.png](https://assets.leetcode.com/users/images/79cbfa7c-4a78-4798-8a8b-83a1447a029d_1693961237.8213181.png)\n
['Two Pointers', 'C++', 'Java', 'Python3', 'JavaScript']