LeetCode每日一题 (59) 328. 奇偶链表

328. 奇偶链表


在这里插入图片描述
在这里插入图片描述


class Solution {
    
    
public:
    ListNode* oddEvenList(ListNode* head) {
    
    
        if(head==nullptr) return head;
        ListNode *first,*second,*temp,*pre;
        first=head;
        second=head->next;
        temp=head->next;
        while(first!=nullptr && second!=nullptr){
    
    
            first->next=first->next->next;
            if(second->next==nullptr) break;
            second->next=second->next->next;
            pre=first;
            first=first->next;
            second=second->next;
        }
        first->next=temp;
        return head;
    }
};

在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/qq_45021180/article/details/109698242