题解 | #链表的奇偶重排#

链表的奇偶重排

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

struct ListNode* oddEvenList(struct ListNode* head ) {
    struct ListNode odd = {0};
    struct ListNode even = {0};
    struct ListNode* p_odd = &odd;
    struct ListNode* p_even = &even;
    struct ListNode* cur = head;
    int count = 1;
    if (head == NULL || head->next == NULL) {
        return head;
    }

    while (cur) {
        if (count % 2) {
            p_odd->next = cur;
            p_odd = p_odd->next;
        } else {
            p_even->next = cur;
            p_even = p_even->next;
        }
        cur = cur->next;
        count++;
    }

    p_even->next = NULL;
    p_odd->next = even.next; 
    return odd.next;
}
全部评论

相关推荐

笑着秋招😊:我一直认为努力有回报是一件很幸福很幸福的事情,恭喜你
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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