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:
The length of root will be in the range [0, 1000].
Each value of a node in the input will be an integer in the range [0, 999].
k will be an integer in the range [1, 50].
Java 与 Python实现
class ListNode{ int val; ListNode next; ListNode (int x){ this.val = x ;} } public class split_links_in_parts { public ListNode[] Solution(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 ,rex = len % k ; ListNode node = root , prev = null; for (int i = 0;node != null && i<k ; i++) { parts[i] = node; for (int j = 0; j< n + (rex >0 ? 1:0);j ++) { prev = node; node = node.next; } prev.next = null; } return parts; } public static ListNode root; public ListNode current; //append node public ListNode addItem(int data){ if (root == null) { root = new ListNode(data); current = root; }else { current.next = new ListNode(data); current = current.next; } return root; } public static void main(String[] args) { ListNode[] parts = new ListNode[5]; split_links_in_parts split= new split_links_in_parts(); for (int i=1;i <=3;i++){ root = split.addItem(i); } System.out.println(root.next.val); parts = split.Solution(root,5); System.out.print("["); for (int i=0;i<5 ;i++){ System.out.print("["); for (ListNode node = parts[i];node!=null;node= node.next){ System.out.print(node.val+" "); } System.out.print("],"); } System.out.println("]"); } }
class Listnode:
def __init__(self,x):
self.val = x
self.next = None
roo ,current= None,None
class Solution:
def addItem(self,data):
global roo,current
if roo==None:
roo = Listnode(data)
current = roo
else:
current.next = Listnode(data)
current = current.next
return roo
def split_list(self,root,k):
"""
:type root: ListNode
:type k: int
:rtype: List[ListNode]
"""
# count link ist ength
cur ,length = root , 0
while cur:
cur , length = cur.next , length+1
n , rex = length // k ,length % k
list_link = [n + 1] * rex + [n] * (k - rex )
# if rex>0: rex=1
# else:rex=0
# prev, cur = None, root
# num_link ,i,n,parts= 0 , 0,n+rex,[0]*k
# while cur != None and i < k:
# parts[i] = cur
# while num_link < n+rex:
# prev , cur = cur , cur.next
# num_link = num_link + 1
# num_link ,i = 0 , i + 1
# prev.next = None
prev ,cur = None ,root
for index ,num in enumerate(list_link):
if prev:
prev.next = None
list_link[index] = cur
for i in range(num):
prev , cur = cur , cur.next
return list_link
if __name__ == '__main__':
solution = Solution()
for data in range(1,4):
root = solution.addItem(data)
list_link_parts = solution.split_list(root,5)
print("[",end=" ")
for i in range(0,5):
node = list_link_parts[i]
print("[", end=" ")
while node:
print(node.val,end=" ")
node = node.next
print("]",end=" ")
print("]", end=" ")