当前位置:网站首页>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;
    }
}
原网站

版权声明
本文为[Dabie Mountains]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020548164671.html