错误提示:
member access within misaligned address 0x000100000001 for type 'struct ListNode', which requires 8
问题代码:
struct ListNode** lstArr = (struct ListNode**)malloc(k * sizeof(struct ListNode*) );
失败了
/**
* 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().
*/
struct ListNode** splitListToParts(struct ListNode* root, int k, int* returnSize) {
// 1. handle null case
if(NULL == root) return NULL;
// 2. allocate memory
struct ListNode* lstArr = (struct ListNode*)malloc(k * sizeof(struct ListNode));
returnSize = (int *)malloc(k * sizeof(int));
for(int i=0; i<k; i++){
lstArr[i].next = NULL;
}
// 3. compute length of list, && splitted list
int nodeLen = 0, minimum = 0, rem = 0;
struct ListNode* pNode = root, *tail = NULL;
while(NULL != pNode) {
nodeLen ++;
pNode = pNode->next;
}
minimum = nodeLen/k, rem = nodeLen%k;
//printf("%d\n", rem);
for(int i=0; i<k; i++){
returnSize[i] = minimum;
}
for(int j=0; j<rem; j++){
returnSize[j] += 1;
}
// 4. split the list
pNode = root;
for(int i=0; i<k; i++){
lstArr[i].val = pNode->val;
if(NULL != pNode)
lstArr[i].next = pNode->next;
int t = returnSize[i];
tail = NULL;
while(NULL != pNode && t>0){
tail = pNode;
pNode = pNode->next;
t--;
}
if(NULL != tail) tail->next = NULL;
}
// 5. return result
return &lstArr;
}