题解 | 链表的奇偶重排

链表的奇偶重排

https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3




struct ListNode* oddEvenList(struct ListNode* head ) {
    // write code here
    if (head == NULL || head->next == NULL) {
        return head;
    }

    struct ListNode* jiHead = head;
    struct ListNode* ouHead = head->next;
    struct ListNode* p = ouHead;
    while(jiHead && jiHead->next && ouHead && ouHead->next){
        jiHead->next = ouHead->next;
        ouHead->next = jiHead->next->next;
        // 向后移动
        jiHead = jiHead->next;
        ouHead = ouHead->next;
    }

    // 将奇节点和偶节点连接
    jiHead->next = p;
    return head;
}

全部评论

相关推荐

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

创作者周榜

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