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

链表的奇偶重排

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

/**
 * struct ListNode {
 *  int val;
 *  struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param head ListNode类
 * @return ListNode类
 */
struct ListNode* oddEvenList(struct ListNode* head ) {
    // write code here
    if(head==NULL)
    return head;
    struct ListNode* head1 = NULL;
    struct ListNode* ptail1 = NULL;
    struct ListNode* head2 = NULL;
    struct ListNode* ptail2 = NULL;
    struct ListNode* p1 = head, *p2 = head->next;
    while (p1) {
        if (head1 == NULL)
            head1 = ptail1 = p1;
        else {
            ptail1->next = p1;
            ptail1 = p1;
        }
        if (head2 == NULL)
            head2 = ptail2 = p2;
        else {
            ptail2->next = p2;
            ptail2 = p2;
        }
        if(p2 == NULL)
        break;
        p1 = p1->next->next;
        if (p2->next != NULL)
            p2 = p2->next->next;
    }
    ptail1->next = head2;
    return head;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 12:05
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 13:05
点赞 评论 收藏
分享
水墨不写bug:疑似没有上过大学
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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