This Leetcode Problem will Challenge Your Mind
Leetcode Problem 725 Explained Visually
Being good at leetcode is essential for all programmers looking to score a high-paying role as a software engineer.
In this article, we will be solving Leetcode problem 725 visually and efficiently.
So without further ado... Let's dive right in!
The Problem
Before we begin, please read this problem statement carefully.
I have highlighted the keywords to make it easier to understand:
Given thehead
of a singly linked list and an integerk
, split the linked list intok
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 thek
parts.
Visual Explanation
Take a look at this diagram to visualize how the linked list is sliced and transferred into the final array of k
parts.
I recommend you read the blue text first which represents the variables and how they change. Then take a look at the arrays and arrows to see how everything connects:

Here is a worded explanation of this diagram:
- Recognize that
k=3
means that the linked list will need to be split into3
contiguous parts, highlighted by the red boxing. - Calculate the length of the linked list which is
5
in this case. - Calculate the minimum part size, which represents the smallest size a red box can be, in this case
1
. - Calculate the remainder when dividing the length of the linked list by
k
, which yields2
in this case. The2
represents how many red boxes will have a size equal to theminimum part size + 1
. - Iterate through the linked list using pointers, take a slice of it, and add this slice to its respective slot in the final array.
The Code
Now that we understand the theory, here is the code written in Python that solves this problem efficiently.
I have also added comments to make it easier to understand:
class Solution:
def splitListToParts(self, head: Optional[ListNode], k: int) -> List[Optional[ListNode]]:
# Base case
if not head:
return [None] * k
headLength = self.lenLinkedList(head)
# "Min" partition size
minPartSize = headLength // k
# Remaining part size
remainder = headLength % k
# Initialize result array to null
result = [None] * k
# Set current to iterate through the list
current = head
# Until we create all k parts
for i in range(k):
# Assign the start of the current part
result[i] = current
# Determine the size of the current part
partSize = minPartSize + (1 if remainder > 0 else 0)
# Advance the pointer by partSize - 1 to reach the end of the current part
for j in range(partSize - 1):
if current:
current = current.next
# If there's more to partition, split the list
if current:
next_part = current.next
current.next = None
current = next_part
# Decrement remainder to ensure correct distribution
if remainder > 0:
remainder -= 1
return result
# Function that gets the length of the input linked list
def lenLinkedList(self, head: ListNode):
i = 0
while head:
i += 1
head = head.next
return i
Wrap Up
I hope you enjoyed this explanation of Leetcode problem 725.
If these are helpful, be sure to let me know in the comments and I'll write more of these.