Split Linked List in Parts LeetCode Solution

Last updated on July 18th, 2024 at 10:13 pm

Here, We see Split Linked List in Parts LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Linked List

Companies

Amazon

Level of Question

Medium

Split Linked List in Parts LeetCode Solution

Split Linked List in Parts LeetCode Solution

Problem Statement

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:

split1 lc

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:

split2 lc

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.

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

class Solution {
public:
    vector<ListNode*> splitListToParts(ListNode* root, int k) {
        vector<ListNode*> parts(k, nullptr);
        int len = 0;
        for (ListNode* node = root; node; node = node->next)
            len++;
        int n = len / k, r = len % k;
        ListNode* node = root, *prev = nullptr;
        for (int i = 0; node && i < k; i++, r--) {
            parts[i] = node;
            for (int j = 0; j < n + (r > 0); j++) {
                prev = node;
                node = node->next;
            }
            prev->next = nullptr;
        }
        return parts;
    }
};

2. Split Linked List in Parts LeetCode Solution Java

class Solution {
    public ListNode[] splitListToParts(ListNode root, int k) {
        ListNode[] parts = new ListNode[k];
        int len = 0;
        ListNode node = root;
        while (node != null) {
            len++;
            node = node.next;
        }
        int n = len / k, r = len % k;
        node = root;
        ListNode prev = null;
        for (int i = 0; node != null && i < k; i++, r--) {
            parts[i] = node;
            for (int j = 0; j < n + (r > 0 ? 1 : 0); j++) {
                prev = node;
                node = node.next;
            }
            if (prev != null) {
                prev.next = null;
            }
        }
        return parts;
    }
}

3. Split Linked List in Parts Solution JavaScript

function ListNode(val, next) {
    this.val = val;
    this.next = next || null;
}

var splitListToParts = function(head, k) {
    const parts = new Array(k).fill(null);
    let len = 0;
    let node = head;
    while (node) {
        len++;
        node = node.next;
    }
    const n = Math.floor(len / k);
    let r = len % k;
    node = head;
    let prev = null;
    for (let i = 0; node && i < k; i++, r--) {
        parts[i] = node;
        for (let j = 0; j < n + (r > 0 ? 1 : 0); j++) {
            prev = node;
            node = node.next;
        }
        if (prev) {
            prev.next = null;
        }
    }
    return parts;
};

4. Split Linked List in Parts Solution Python

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def splitListToParts(self, head, k):
        parts = [None] * k
        len = 0
        node = head
        while node:
            len += 1
            node = node.next
        n, r = divmod(len, k)
        node = head
        prev = None
        for i in range(k):
            parts[i] = node
            for j in range(n + (1 if r > 0 else 0)):
                prev = node
                node = node.next
            if prev:
                prev.next = None
            if r > 0:
                r -= 1
        return parts
Scroll to Top