当前位置:网站首页>Leetcode question brushing series - mode 2 (datastructure linked list) - 725 (m): split linked list in parts
Leetcode question brushing series - mode 2 (datastructure linked list) - 725 (m): split linked list in parts
2022-06-11 04:37:00 【Dabie Mountains】
leetcode Brush questions series ---- Pattern 2(Datastructure Linked list )- 725(M):Split Linked List in Parts Split the list
Tips
- For more information, please refer to the catalogue of this series
- Master the definition of linked list , Remember that the tail node must point to null . When typing code , The brain thinks about the direction of the pointer .
- Here, the number of nodes is evenly distributed , There is a modular operation , The remainder should be less than the fraction , Then, each partition whose preceding value is less than or equal to the remainder gets an extra node .part_szie = q +(1 if i<r else 0)
Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def splitListToParts(self, head: ListNode, k: int) -> List[ListNode]:
temp = head
count = 0
while temp:
count += 1
temp = temp.next
q, r = count//k, count%k
parts = [None for _ in range(k)]
i, current = 0, head
while i<k and current:
parts[i] = current
part_size = q+(1 if i<r else 0)
for item in range(part_size-1): current = current.next
next_part = current.next
current.next = None
current = next_part
i += 1
return parts
C++
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
vector<ListNode*> splitListToParts(ListNode* head, int k) {
ListNode* temp = head;
int count = 0;
while(temp!=nullptr){
count+=1; temp=temp->next;}
int q = count/k;
int r = count%k;
vector<ListNode*> parts(k, nullptr);
ListNode* current = head;
for(int i=0; i<k && current!=nullptr; i++)
{
parts[i] = current;
int part_size = q + (i<r ? 1 : 0);
for(int j=1; j<part_size; j++) current=current->next;
ListNode* next_part = current->next;
current->next = nullptr;
current = next_part;
}
return parts;
}
};
C#
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */
public class Solution {
public ListNode[] SplitListToParts(ListNode head, int k) {
ListNode temp = head;
int count = 0;
while(temp!=null){
count+=1; temp=temp.next;}
int q = count/k;
int r = count%k;
ListNode[] parts = new ListNode[k];
ListNode current = head;
for(int i=0; i<k && current!=null; i++)
{
parts[i] = current;
int part_size = q + (i<r ? 1 : 0);
for(int j=1; j<part_size; j++) current=current.next;
ListNode next_part = current.next;
current.next = null;
current = next_part;
}
return parts;
}
}
Java
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */
class Solution {
public ListNode[] splitListToParts(ListNode head, int k) {
ListNode temp = head;
int count = 0;
while(temp!=null){
count+=1; temp=temp.next;}
int q = count/k;
int r = count%k;
ListNode[] parts = new ListNode[k];
ListNode current = head;
for(int i=0; i<k && current!=null; i++)
{
parts[i] = current;
int part_size = q + (i<r ? 1 : 0);
for(int j=1; j<part_size; j++) current=current.next;
ListNode next_part = current.next;
current.next = null;
current = next_part;
}
return parts;
}
}
边栏推荐
- Do you know the difference between mallbook ledger and bank ledger?
- idea gradle项目 如何导入 本地jar 包
- Unity 音乐播放管理器
- Sorting out relevant programming contents of renderfeature of unity's URP
- An adaptive chat site - anonymous online chat room PHP source code
- Decision tree (hunt, ID3, C4.5, cart)
- codesys 获取系统时间
- 记一次ES 事故
- 2021 5g aiot annual innovation achievements! release!
- Best practices and principles of lean product development system
猜你喜欢

An adaptive chat site - anonymous online chat room PHP source code

Unity occlusion culling

Exness: Liquidity Series - order Block, Unbalanced (II)

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码

Guanghetong "carbon" seeking green sharing 5g/lte module solution for two rounds of travel

Unity 传送机制的实现

Unity 音乐播放管理器

Unity 编辑器扩展 保存位置

Guanghetong officially released the sc126 series of intelligent modules to promote more intelligent connection

Unity music playback Manager
随机推荐
Programming Examples Using RDMA Verbs
Feature engineering feature dimension reduction
The live broadcast helped Hangzhou e-commerce Unicorn impact the listing, and the ledger system restructured the new pattern of e-commerce transactions
USB转232 转TTL概述
Analysis of zero time technology | discover lightning loan attack
Unity Editor Extension save location
国际期货黄金手续费怎么算?
Guanghetong LTE CAT6 module fm101-cg, which supports CBRS band, took the lead in obtaining FCC certification
Discussion on the construction of Fuzhou nucleic acid testing laboratory
Split all words into single words and delete the product thesaurus suitable for the company
JVM (3): classloader classification and parental delegation mechanism
芯源cw32f030c8t6用keil5编译时出现的问题
meedu知识付费解决方案 v4.5.4源码
Unity 传送机制的实现
Guanghetong officially released the annual theme of 2022 5g Huanxin: Five Forces co drive · Jizhi future
用万用表检测数码管
How to quickly find the official routine of STM32 Series MCU
Google Code Coverage best practices
Unity 伤害值的显示
Meedu knowledge payment solution v4.5.4 source code