Dsa
Linked List
Medium
Split a Linked List into Parts
You are given a linked list and an integer k. Your task is to split the linked list into k consecutive parts. Each part should be as equal as possible in terms of the number of nodes. If there are remaining nodes, they should be distributed from the front of the parts. The program should output the sizes of each part. Write a program that reads the linked list and the value of k, and prints the sizes of each part in a space-separated format.
Input Format:
- The first line contains a series of integers representing the linked list nodes.
- The second line contains an integer k (1 <= k <= length of linked list).
Output Format:
- A space-separated string of integers where each integer represents the size of each part.
Example:
Input:
1 2 3 4 5 -1
3
Output:
2 2 1
Key concepts
linked_listpartitioning
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now