725. Split Linked List in Parts
Difficulty: Medium
Topics: Linked List
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:
Example 2:
Constraints:
Hint:
Solution:
The key observation is that the number of nodes in each part should not differ by more than 1. This means:
Let's implement this solution in PHP: 725. Split Linked List in Parts
<?php // Definition for singly-linked list. class ListNode { public $val = 0; public $next = null; function __construct($val = 0, $next = null) { $this->val = $val; $this->next = $next; } } /** * @param ListNode $head * @param Integer $k * @return ListNode[] */ function splitListToParts($head, $k) { ... ... ... /** * go to ./solution.php */ } // Helper function to create a linked list from an array function createLinkedList($arr) { $head = new ListNode($arr[0]); $current = $head; for ($i = 1; $i < count($arr); $i++) { $current->next = new ListNode($arr[$i]); $current = $current->next; } return $head; } // Helper function to print a linked list function printList($head) { $result = []; while ($head !== null) { $result[] = $head->val; $head = $head->next; } return $result; } // Test case 1 $head = createLinkedList([1, 2, 3]); $k = 5; $result = splitListToParts($head, $k); foreach ($result as $part) { print_r(printList($part)); } // Test case 2 $head = createLinkedList([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); $k = 3; $result = splitListToParts($head, $k); foreach ($result as $part) { print_r(printList($part)); } ?> <h3> Explanation: </h3> <ol> <li><p><strong>Calculate Length</strong>: We first traverse the linked list to find its length.</p></li> <li> <p><strong>Determine Parts</strong>:</p> <ul> <li>We calculate part_size as length // k, which gives the minimum size each part should have.</li> <li>We calculate extra_nodes as length % k, which gives the number of parts that should have one extra node.</li> </ul> </li> <li> <p><strong>Split the List</strong>: We loop through k parts and split the list:</p> <ul> <li>For each part, assign part_size + 1 nodes if it should have an extra node, otherwise just part_size.</li> <li>At the end of each part, we break the link to the rest of the list.</li> </ul> </li> <li><p><strong>Handle Null Parts</strong>: If there are fewer nodes than k, the remaining parts will be null (empty).</p></li> </ol> <h3> Test Cases </h3> <ul> <li> <strong>Example 1</strong>: </li> </ul> <pre class="brush:php;toolbar:false"> $head = [1,2,3]; $k = 5; Output: [[1],[2],[3],[],[]]
$head = [1,2,3,4,5,6,7,8,9,10]; $k = 3; Output: [[1,2,3,4],[5,6,7],[8,9,10]]
This solution efficiently splits the linked list into k parts with a time complexity of (O(n + k)), where n is the length of the list.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks ?. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
The above is the detailed content of . Split Linked List in Parts. For more information, please follow other related articles on the PHP Chinese website!