725. Split Linked List in Parts LeetCode Solution

In this guide, you will get 725. Split Linked List in Parts LeetCode Solution with the best time and space complexity. The solution to Split Linked List in Parts problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Split Linked List in Parts solution in C++
  4. Split Linked List in Parts solution in Java
  5. Split Linked List in Parts solution in Python
  6. Additional Resources
725. Split Linked List in Parts LeetCode Solution image

Problem Statement of Split Linked List in Parts

Given the head of a singly linked list and an integer k, split the linked list into k consecutive linked list parts.
The length of each part should be as equal as possible: no two parts should have a size differing by more than one. This may lead to some parts being null.
The parts should be in the order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal to parts occurring later.
Return an array of the k parts.

Example 1:

Input: head = [1,2,3], k = 5
Output: [[1],[2],[3],[],[]]
Explanation:
The first element output[0] has output[0].val = 1, output[0].next = null.
The last element output[4] is null, but its string representation as a ListNode is [].

Example 2:

Input: head = [1,2,3,4,5,6,7,8,9,10], k = 3
Output: [[1,2,3,4],[5,6,7],[8,9,10]]
Explanation:
The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.

See also  607. Sales Person LeetCode Solution

Constraints:

The number of nodes in the list is in the range [0, 1000].
0 <= Node.val <= 1000
1 <= k <= 50

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

725. Split Linked List in Parts LeetCode Solution in C++

class Solution {
 public:
  vector<ListNode*> splitListToParts(ListNode* root, int k) {
    vector<ListNode*> ans(k);
    const int length = getLength(root);
    const int subLength = length / k;
    int remainder = length % k;

    ListNode* prev = nullptr;
    ListNode* head = root;

    for (int i = 0; i < k; ++i, --remainder) {
      ans[i] = head;
      for (int j = 0; j < subLength + (remainder > 0); ++j) {
        prev = head;
        head = head->next;
      }
      if (prev != nullptr)
        prev->next = nullptr;
    }

    return ans;
  }

 private:
  int getLength(ListNode* root) {
    int length = 0;
    for (ListNode* curr = root; curr; curr = curr->next)
      ++length;
    return length;
  }
};
/* code provided by PROGIEZ */

725. Split Linked List in Parts LeetCode Solution in Java

class Solution {
  public ListNode[] splitListToParts(ListNode root, int k) {
    ListNode[] ans = new ListNode[k];
    final int length = getLength(root);
    final int subLength = length / k;
    int remainder = length % k;

    ListNode prev = null;
    ListNode head = root;

    for (int i = 0; i < k; ++i, --remainder) {
      ans[i] = head;
      for (int j = 0; j < subLength + (remainder > 0 ? 1 : 0); ++j) {
        prev = head;
        head = head.next;
      }
      if (prev != null)
        prev.next = null;
    }

    return ans;
  }

  private int getLength(ListNode root) {
    int length = 0;
    for (ListNode curr = root; curr != null; curr = curr.next)
      ++length;
    return length;
  }
}
// code provided by PROGIEZ

725. Split Linked List in Parts LeetCode Solution in Python

class Solution:
  def splitListToParts(self, root: ListNode, k: int) -> list[ListNode]:
    ans = [[] for _ in range(k)]
    length = 0
    curr = root
    while curr:
      length += 1
      curr = curr.next
    subLength = length // k
    remainder = length % k

    prev = None
    head = root

    for i in range(k):
      ans[i] = head
      for j in range(subLength + (1 if remainder > 0 else 0)):
        prev = head
        head = head.next
      if prev:
        prev.next = None
      remainder -= 1

    return ans
# code by PROGIEZ

Additional Resources

See also  1089. Duplicate Zeros LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.