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

链表的奇偶重排

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

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @return ListNode类
 */
#include <stdbool.h>
struct ListNode* oddEvenList(struct ListNode* head ) {
    // write code here
    if(head==NULL||head->next==NULL||head->next->next==NULL){
        return head;
    }
    struct ListNode *head1=head,*head2=head->next;
    struct ListNode *p=head1,*q=head2,*t=head->next->next;
    bool flag=true;
    while (t!=NULL) {
        if(flag){
            p->next=t;
            p=p->next;
        }else{
            q->next=t;
            q=q->next;
        }
        t=t->next;
        flag=!flag;
        p->next=NULL;
        q->next=NULL;
    }
    p->next=head2;
    return head1;
}

全部评论

相关推荐

10-28 17:30
已编辑
华东交通大学 Java
iori2333:这太正常了 我字节面了四五轮 没有一次是在官网投递 都是hr主动捞
秋招笔试记录
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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