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

链表的奇偶重排

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

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        // write code here
        if( head==nullptr || 
            head->next==nullptr ||    
            head->next->next==nullptr)
            return head;
        ListNode* cur = head->next->next;
        ListNode* once_ou = head->next;
        ListNode* ji=head;
        ListNode* ou = head->next;
        int count=3;
        while(cur)
        {
           if(count & 1){
                ji->next = cur;
                ji = cur;
           }else{
                ou->next = cur;
                ou=cur;
           }
           ++count;
           cur = cur->next;
        }
        ou->next = nullptr;
        ji->next = once_ou;
        return head;
    }
};

全部评论

相关推荐

牛客773130651号:巨佬,简历模板换成上下的,左右的很烦,hr看着不爽。。。科大随便乱杀,建议能保研就保研,不行也得考一下 ,985硕去干算法,比开发强多了。开发许多双非都能搞,学历优势用不上,算法有门槛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务