leetcode725. Delimited linked list (java)

Question description

Given a singly linked list with head node as head and an integer k, please design an algorithm to divide the linked list into k consecutive parts.
The length of each part should be as equal as possible: the length of any two parts should not differ by more than 1. This may result in some parts being null.
The k parts should be arranged in the order they appear in the linked list, and the length of the first part should be greater than or equal to the length of the later part.
Returns an array consisting of the k parts above.

Example 1:
Insert image description here
Input: head = [1,2,3], k = 5
Output: [[1],[2],[3],[],[]]
Explanation:
The first element output[0] is output[0].val = 1, output[0].next = null.
The last element, output[4], is null, but its string representation as a ListNode is [].

Example 2:
Insert image description hereInput: head = [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 is divided into several consecutive parts, and the length of each part differs by no more than 1. The length of the front part is greater than or equal to the length of the back part.

Tip:
The number of nodes in the linked list is in the range [0, 1000]
0 <= Node.val <= 1000
1 <= k <= 50
Insert image description here

split linked list

The question requires dividing the given linked list into kkk consecutive parts. Since the length of each divided part is related to the length of the original linked list, it is necessary to first traverse the linked list to obtain the length nnn of the linked list.
After getting the length n of the linked list, record quotient=⌊n / k⌋, remainder=n % k. Then among the k parts separated, the length of the first remaining parts is quotient+1, and the length of each remaining part is Each is quotient.
When dividing the linked list, start traversing from the head node of the linked list, remember the current node as curr, and perform the following operations for each part:

  1. Use curr as the head node of the current part;
  2. Calculate the length of the current part partSize;
  3. Move curr backward partSize steps, then curr is the end node of the current part;
  4. When curr reaches the end node of the current part, the connection relationship between curr and the next node needs to be split, and the next node of curr needs to be stored before splitting;
  5. Let the next pointer of curr point to nul to complete the split of curr and next;
  6. Assign next to curr.

code demo

/**
 * 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) {
    
    
        int n = 0;
        ListNode temp = head;
        while (temp != null) {
    
    
            n++;
            temp = temp.next;
        }
        int quotient = n / k, remainder = n % k;

        ListNode[] parts = new ListNode[k];
        ListNode curr = head;
        for (int i = 0; i < k && curr != null; i++) {
    
    
            parts[i] = curr;
            int partSize = quotient + (i < remainder ? 1 : 0);
            for (int j = 1; j < partSize; j++) {
    
    
                curr = curr.next;
            }
            ListNode next = curr.next;
            curr.next = null;
            curr = next;
        }
        return parts;
    }

}

Guess you like

Origin blog.csdn.net/SP_1024/article/details/132907438