Beats Everyone (100%) || Line by Line explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nequal=/\nextra=%\n# Approach\n Describe your approach to solving the problem. \nsame as
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nequal=/\nextra=%\n# Approach\n<!-- Describe your approach to solving the problem. -->\nsame as mean in dice problem(recent POTD)\n![IMG_20240908_130848.jpg](https://assets.leetcode.com/users/images/818c16ed-c657-40df-ac3a-6072d460f101_1725781736.9597914.jpeg)\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)->Auxiliary Space\nO(k)->Total SPace\n# Code\n```cpp []\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* head, int k) {\n        vector<ListNode*>ans;\n        ListNode* temp=head;\n        int c=0;\n        while(temp){\n            c++;\n            temp=temp->next;\n        }\n        int equal=c/k,extra=c%k;\n        ListNode* prevHead=head;\n        while(k--){\n            int t=equal;\n            if(extra>0) t++;\n            t--;\n            ListNode* newHead=prevHead;\n            ListNode* temp=newHead;\n            while(newHead && t--){\n                newHead=newHead->next;\n            }\n            if(newHead){\n                prevHead=newHead->next;\n                newHead->next=NULL;\n            }\n            ans.push_back(temp);\n            extra--;\n        }\n        return ans;\n    }\n};\n```\n```javascript []\nclass ListNode {\n    constructor(val = 0, next = null) {\n        this.val = val;\n        this.next = next;\n    }\n}\n\nclass Solution {\n    splitListToParts(head, k) {\n        let ans = [];\n        let temp = head;\n        let count = 0;\n        \n        while (temp) {\n            count++;\n            temp = temp.next;\n        }\n        \n        let equal = Math.floor(count / k);\n        let extra = count % k;\n        \n        let prevHead = head;\n        \n        while (k--) {\n            let t = equal;\n            if (extra > 0) t++;\n            t--;\n            \n            let newHead = prevHead;\n            let current = newHead;\n            \n            while (newHead && t--) {\n                newHead = newHead.next;\n            }\n            \n            if (newHead) {\n                prevHead = newHead.next;\n                newHead.next = null;\n            }\n            \n            ans.push(current);\n            extra--;\n        }\n        \n        return ans;\n    }\n}\n\n```\n```python []\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, head: ListNode, k: int):\n        ans = []\n        temp = head\n        count = 0\n        \n        while temp:\n            count += 1\n            temp = temp.next\n        \n        equal = count // k\n        extra = count % k\n        \n        prevHead = head\n        \n        while k > 0:\n            t = equal\n            if extra > 0:\n                t += 1\n                extra -= 1\n            else:\n                t -= 1\n            \n            newHead = prevHead\n            for _ in range(t - 1):\n                if newHead:\n                    newHead = newHead.next\n            \n            if newHead:\n                nextHead = newHead.next\n                newHead.next = None\n                prevHead = nextHead\n            \n            ans.append(prevHead)\n            k -= 1\n        \n        return ans\n\n```\n```ruby []\nclass ListNode\n  attr_accessor :val, :next\n\n  def initialize(val = 0, next_node = nil)\n    @val = val\n    @next = next_node\n  end\nend\n\nclass Solution\n  def split_list_to_parts(head, k)\n    ans = []\n    temp = head\n    count = 0\n\n    while temp\n      count += 1\n      temp = temp.next\n    end\n\n    equal = count / k\n    extra = count % k\n\n    prev_head = head\n\n    while k > 0\n      t = equal\n      if extra > 0\n        t += 1\n        extra -= 1\n      else\n        t -= 1\n      end\n\n      new_head = prev_head\n      (t - 1).times do\n        new_head = new_head.next if new_head\n      end\n\n      if new_head\n        next_head = new_head.next\n        new_head.next = nil\n        prev_head = next_head\n      end\n\n      ans << prev_head\n      k -= 1\n    end\n\n    ans\n  end\nend\n\n```\n```java []\nimport java.util.ArrayList;\nimport java.util.List;\n\nclass ListNode {\n    int val;\n    ListNode next;\n    ListNode() { this.val = 0; this.next = null; }\n    ListNode(int x) { this.val = x; this.next = null; }\n    ListNode(int x, ListNode next) { this.val = x; this.next = next; }\n}\n\nclass Solution {\n    public List<ListNode> splitListToParts(ListNode head, int k) {\n        List<ListNode> ans = new ArrayList<>();\n        ListNode temp = head;\n        int count = 0;\n\n        while (temp != null) {\n            count++;\n            temp = temp.next;\n        }\n\n        int equal = count / k;\n        int extra = count % k;\n\n        ListNode prevHead = head;\n\n        while (k-- > 0) {\n            int t = equal;\n            if (extra > 0) {\n                t++;\n                extra--;\n            }\n            ListNode newHead = prevHead;\n            ListNode current = newHead;\n            while (newHead != null && t-- > 1) {\n                newHead = newHead.next;\n            }\n\n            if (newHead != null) {\n                prevHead = newHead.next;\n                newHead.next = null;\n            }\n            ans.add(current);\n        }\n\n        return ans;\n    }\n}\n```\n\n------------------------------------------------\n> I Hope you understood !!\nPls Upvote\n\n
['Linked List', 'C++', 'Java', 'Python3']