题解 | #重排链表#

重排链表

https://www.nowcoder.com/practice/3d281dc0b3704347846a110bf561ef6b

/**
 * 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;
}




全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务