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

链表的奇偶重排

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;
}
全部评论

相关推荐

投递华为等公司10个岗位
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务