https://leetcode.com/problems/split-linked-list-in-parts/description/
The length of
Each value of a node in the input will be an integer in the range
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode cur = root;
int N = 0;
while (cur != null) {
cur = cur.next;
N++;
}
int width = N / k, rem = N % k;
ListNode[] ans = new ListNode[k];
cur = root;
for (int i = 0; i < k; ++i) {
ListNode head = cur;
for (int j = 0; j < width + (i < rem ? 1 : 0) - 1; ++j) {
if (cur != null) cur = cur.next;
}
if (cur != null) {
ListNode prev = cur;
cur = cur.next;
prev.next = null;
}
ans[i] = head;
}
return ans;
}
https://leetcode.com/problems/split-linked-list-in-parts/discuss/109296/JavaC++-Clean-Code
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode cur = root;
int N = 0;
while (cur != null) {
cur = cur.next;
N++;
}
int width = N / k, rem = N % k;
ListNode[] ans = new ListNode[k];
cur = root;
for (int i = 0; i < k; ++i) {
ListNode head = new ListNode(0), write = head;
for (int j = 0; j < width + (i < rem ? 1 : 0); ++j) {
write = write.next = new ListNode(cur.val);
if (cur != null) cur = cur.next;
}
ans[i] = head.next;
}
return ans;
}
LinkedList: Use sentinel head node
X.
https://leetcode.com/problems/split-linked-list-in-parts/discuss/109281/A-solution-without-knowing-the-length-of-linkedlist
Every time the pointer slow moves one step forward, the pointer fast moves k steps (or stop at the end of the list as nullptr). So we expect slow moves n / k steps before fast stops, here n is the length of the list, including boundary adjust. But I think the approach does much more "pointer move" ( O(n2) ) than others which measure the length of the list at first
Given a (singly) linked list with head node
root
, write a function to 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 1. This may lead to some parts being null.
The parts should be in order of occurrence in the input list, and parts occurring earlier should always have a size greater than or equal parts occurring later.
Return a List of ListNode's representing the linked list parts that are formed.
Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ]
Example 1:
Input: root = [1, 2, 3], k = 5 Output: [[1],[2],[3],[],[]] Explanation: The input and each element of the output are ListNodes, not arrays. For example, the input root has root.val = 1, root.next.val = 2, \root.next.next.val = 3, and root.next.next.next = null. The first element output[0] has output[0].val = 1, output[0].next = null. The last element output[4] is null, but it's string representation as a ListNode is [].
Example 2:
Input: root = [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.
Note:
root
will be in the range [0, 1000]
.[0, 999]
.k
will be an integer in the range [1, 50]
.public ListNode[] splitListToParts(ListNode root, int k) {
ListNode cur = root;
int N = 0;
while (cur != null) {
cur = cur.next;
N++;
}
int width = N / k, rem = N % k;
ListNode[] ans = new ListNode[k];
cur = root;
for (int i = 0; i < k; ++i) {
ListNode head = cur;
for (int j = 0; j < width + (i < rem ? 1 : 0) - 1; ++j) {
if (cur != null) cur = cur.next;
}
if (cur != null) {
ListNode prev = cur;
cur = cur.next;
prev.next = null;
}
ans[i] = head;
}
return ans;
}
https://leetcode.com/problems/split-linked-list-in-parts/discuss/109296/JavaC++-Clean-Code
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode[] parts = new ListNode[k];
int len = 0;
for (ListNode node = root; node != null; node = node.next)
len++;
int n = len / k, r = len % k; // n : minimum guaranteed part size; r : extra nodes spread to the first r parts;
ListNode node = root, 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;
}
prev.next = null;
}
return parts;
}
public ListNode[] splitListToParts(ListNode root, int k) {
int size = getSize(root);
ListNode[] result = new ListNode[k];
while (k > 0 && root != null) {
int subSize = ceilDiv(size, k);
size -= subSize; // here
ListNode subRoot = root;
result[result.length - k] = subRoot;
// the way to use subSize is error prone, maybe just use a different variable
while (subSize > 1 && root != null) { // not >0
root = root.next;
subSize--;
}
// root maybe null now. last time
// cut it off
if (root != null) {
ListNode newRoot = root.next;
root.next = null;
root = newRoot;
}
// size -= subSize; not here
--k;
}
while (k > 0) { // merge the case with the previous while loop
result[result.length - k] = null;
--k;
}
return result;
}
// x/y
private static int ceilDiv(int x, int y) {
if (x % y == 0)
return x / y;
return x / y + 1;
}
private int getSize(ListNode root) {
int size = 0;
while (root != null) {
++size;
root = root.next;
}
return size;
}
Approach #1: Create New Lists
- Time Complexity: , where is the number of nodes in the given list. If is large, it could still require creating many new empty lists.
- Space Complexity: , the space used in writing the answer.
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode cur = root;
int N = 0;
while (cur != null) {
cur = cur.next;
N++;
}
int width = N / k, rem = N % k;
ListNode[] ans = new ListNode[k];
cur = root;
for (int i = 0; i < k; ++i) {
ListNode head = new ListNode(0), write = head;
for (int j = 0; j < width + (i < rem ? 1 : 0); ++j) {
write = write.next = new ListNode(cur.val);
if (cur != null) cur = cur.next;
}
ans[i] = head.next;
}
return ans;
}
LinkedList: Use sentinel head node
X.
https://leetcode.com/problems/split-linked-list-in-parts/discuss/109281/A-solution-without-knowing-the-length-of-linkedlist
Every time the pointer slow moves one step forward, the pointer fast moves k steps (or stop at the end of the list as nullptr). So we expect slow moves n / k steps before fast stops, here n is the length of the list, including boundary adjust. But I think the approach does much more "pointer move" ( O(n2) ) than others which measure the length of the list at first
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode[] res = new ListNode[k];
int step = k;
for(int i = 0; i < k; i++) {
if(root == null) break;
ListNode slow = root;
ListNode fast = root;
while(true) {
fast = move(fast, step);
if(fast != null) {
slow = slow.next;
} else {
break;
}
}
res[i] = root;
if(slow.next != null) {
root = slow.next;
slow.next = null;
} else {
break;
}
--step;
}
return res;
}
private ListNode move(ListNode node, int step) {
while(step > 0) {
node = node.next;
step--;
if(node == null) break;
}
return node;
}
}