首页 > 其他分享 >链表--链表平均分割成几个子链表的方案

链表--链表平均分割成几个子链表的方案

时间:2022-10-26 21:08:21浏览次数:50  
标签:Current 分割 ListNode -- next 链表 int parts root


725. Split Linked List in Parts

Medium

36682FavoriteShare

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.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* root, int k) {
int Length = 0;
vector<ListNode*> Result(k,NULL);
for(ListNode* i = root;i != NULL;i = i->next){
Length ++;
}
ListNode* Current = root;
int mean = Length / k;
int shift = Length % k;
for(int i = 0;i < k && Current;i ++){
Result[i] = Current;
for(int j = 1;j < mean + (i < shift);j ++){
Current = Current->next;
}
ListNode* tmp = Current->next;
Current->next = NULL;
Current = tmp;
}
return Result;
}
};

 

标签:Current,分割,ListNode,--,next,链表,int,parts,root
From: https://blog.51cto.com/u_13121994/5798502

相关文章

  • 栈的使用以及括号匹配扩展
    678. ValidParenthesisStringMedium68824FavoriteShareGivenastringcontainingonlythreetypesofcharacters:'(',')'and'*',writeafunctiontocheckwhet......
  • 优先队列--著名的TopK问题(最小堆的使用)
    692. TopKFrequentWordsMedium77671FavoriteShareGivenanon-emptylistofwords,returnthe k mostfrequentelements.Youranswershouldbesortedbyfrequen......
  • 一个字符串用空格作为分隔符,可以用while(cin>>Input)进行输入
    题目描述给定一个句子(只包含字母和空格),将句子中的单词位置反转,单词用空格分割,单词之间只有一个空格,前后没有空格。比如:(1)“helloxiaomi”->“mixiaohello”输入描......
  • 从小到大排序
    题目描述六一儿童节,老师带了很多好吃的巧克力到幼儿园。每块巧克力j的重量为w[j],对于每个小朋友i,当他分到的巧克力大小达到h[i](即w[j]>=h[i]),他才会上去表演节目。老师的......
  • 字典树--字典树题母
    648. ReplaceWordsMedium457110FavoriteShareInEnglish,wehaveaconceptcalled ​​root​​​,whichcanbefollowedbysomeotherwordstoformanotherlong......
  • 连线问题(数学题)
    题目描述某一天,Alice比较无聊,于是她为自己发明了一个游戏玩。首先她在纸上画了一个圆,然后从这个圆的圆弧上均匀地取出n个点,这n个点将圆n等分。接下来,Alice每次从这......
  • 倒序排序求次数平方和最大值
    题目描述有一种有趣的字符串价值计算方式:统计字符串中每种字符出现的次数,然后求所有字符次数的平方和作为字符串的价值例如:字符串"abacaba",里面包括4个'a',2个'b',1个......
  • 贪心找零钱
    题目描述楚乔、宇文玥和燕洵在日本旅行,经过了几天的游玩之后,钱包里出现了大量硬币,楚乔决定用钱包里的硬币为宇文玥和燕洵在自动贩卖机买水。楚乔的钱包里有1元、5元、10元、......
  • 有时间再研究吧
    #include<bits/stdc++.h>usingnamespacestd;structpt{intx;intpos;pt(inta,intb):x(a),pos(b){}};structcmp{//重写比较函数booloperator......
  • 利用map对数组中的元素及其下标进行存储
    题目描述小摩有一个N个数的数组,他想将数组从小到大排好序,但是萌萌的小摩只会下面这个操作:任取数组中的一个数然后将它放置在数组的最后一个位置。问最少操作多少次可以使得......