Leecode 725 Split Linked List in Parts

Leecode 725 Split Linked List in Parts

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.

Note:

The length of root will be in the range [0, 1000].

Each value of a node in the input will be an integer in the range [0, 999].

k will be an integer in the range [1, 50].

题意

  给出一个单链表,和一个数字k,要求把这个单链表分成k部分,并要求每个部分元素个数的差不超过1,个数多的放前面。

解题思路

  方法想起来倒是不麻烦,先把能平均分配的部分算出来,再算出剩余的部分,最后再按顺序分配即可。附上C语言和C++的代码。

代码
/*C*/
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int getLen(struct ListNode* p)
{
    int len=0;
    struct ListNode* l=p;
    while(p)
    {
        p=p->next;
        len++;
    }
    return len;
}

struct ListNode** splitListToParts(struct ListNode* root, int k,int* returnSize)
{
    int len=getLen(root);
    int width=len/k;
    int rest=len%k;

    struct ListNode** v=(struct ListNode**)malloc(50*sizeof(struct ListNode*));
    for(int i=0; i<50; i++) v[i]=NULL;

    for(int i=0; i<k; i++)
    {
        if(!root) break;
        struct ListNode* ptr=(struct ListNode*)malloc(sizeof(struct ListNode));
        struct ListNode* head=ptr;
        for(int j=0; j<width+(i<rest?1:0); j++)
        {
            struct ListNode* p=(struct ListNode*)malloc(sizeof(struct ListNode));
            p->val=root->val;

            p->next=NULL;
            root=root->next;

            ptr->next=p;
            ptr=ptr->next;
            if(!root) break;
        }
        v[i]=head->next;
    }
    *returnSize=k;
    return v;
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
   int getlength(ListNode* root)
    {
        int length=0;
        while(root) length++,root=root->next;
        return length;
    }
    vector<ListNode*> splitListToParts(ListNode* root, int k)
    {
        int length=getlength(root);
        int width=length/k;
        int rest=length%k;

        vector<ListNode*> v;
        for(int i=0; i<k; i++) v.push_back(NULL);

        for(int i=0; i<k; i++)
        {
            if(!root) break;
            ListNode* ptr=(ListNode*)malloc(sizeof(ListNode));
            ListNode* head=ptr;
            for(int j=0; j<width+(i<rest?1:0); j++)
            {
                ListNode* p=(ListNode*)malloc(sizeof(ListNode));
                p->val=root->val;

                p->next=NULL;
                root=root->next;

                ptr->next=p;
                ptr=ptr->next;
            }
            v[i]=head->next;
        }
        return v;
    }
};
写在后面

  太久没写过跟指针相关的代码了,写起来真的吃力,以后最好是每天写一题保持手感,这样对复习也有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值