原题题目:
- 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.
首先看到题目我们可以了解到,是要将链表分成k个部分,但是每个部分的长度相差不能超过1,而且长的部分要出现在前面。 - 首先,我们通过获取链表中节点的数目sum,对k进行sum/k和sum%k两个操作,获得每个部分相应的长度。由于稍微长一点的链表应该出现在前面,因此我们可以通过
int curSize = size + (mod-- > 0 ? 1 : 0);
获取链表相应的长度,正好长的在前面,由于是对k取余因此多出来的几个1一定小于新建的用来存储各链表的数组长度。
- 我们这里数组存储的是各个部分链表的头结点,就像是HashMap的存储原理,当出现哈希值一样的元素的时候,我们就以链表的形式将他们捆绑在一起,并且将链表的头结点存储在数组内。就假如原链表{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},k=3,存储的结果应该如下所示:
附上代码:
public static ListNode[] splitListToParts(ListNode root, int k){
ListNode cur = root;
int sum = 0;
while(cur != null){
sum ++;
cur = cur.next;
}
int m = sum / k;
int n = sum % k;
cur = root;
ListNode[] res = new ListNode[k];
for(int i = 0; i < k && cur != null; i ++){
res[i] = cur;
int temp = m + (n-- > 0 ? 1 : 0);
while(temp-- > 1)
cur = cur.next;
ListNode next = cur.next;
cur.next = null;
cur = next;
}
return res;
}
public static void main(String[] args) {
ListNode a = new ListNode(1);
ListNode b = new ListNode(2);
ListNode c = new ListNode(3);
ListNode d = new ListNode(4);
ListNode e = new ListNode(5);
ListNode f = new ListNode(6);
ListNode g = new ListNode(7);
ListNode h = new ListNode(8);
ListNode i = new ListNode(9);
ListNode j = new ListNode(10);
a.next = b;
b.next = c;
c.next = d;
d.next = e;
e.next = f;
f.next = g;
g.next = h;
h.next = i;
i.next = j;
ListNode[] res = splitListToParts(a, 3);
for(ListNode z : res){
ListNode y = z;
while(y != null){
if(y.next == null)
System.out.print(y.val);
else
System.out.print(y.val + "->");
y = y.next;
}
System.out.println();
}
}
结果: