每天一道LeetCode >> 分隔链表(java)

原题题目:

  • 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();
		}
	}

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值