题解 | #重排链表#

重排链表

https://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b?tpId=196&tqId=37046&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=undefined&judgeStatus=undefined&tags=&title=

/**

* struct ListNode {

* int val;

* struct ListNode *next;

* };

*/

/**

*

* @param head ListNode类

* @return void

*/

void reorderList(struct ListNode* head ) {

int nodeNum = 0;

int i = 0;

struct ListNode *temp = head;

struct ListNode **nodeArry = NULL;

if ((head == NULL) || (head->next == NULL)){

return;

}

while (temp != NULL){

nodeNum++;

nodeArry = (struct ListNode **)realloc(nodeArry, (nodeNum * sizeof(struct ListNode *)));

nodeArry[(nodeNum - 1)] = temp;

temp = temp->next;

}

for (i = nodeNum - 1; i >= nodeNum / 2; i--){

nodeArry[nodeNum - 1 - i]->next = nodeArry[i];

nodeArry[i]->next = nodeArry[nodeNum - i];

}

nodeArry[i + 1]->next = NULL;

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务