My recursive solution in C++


  • -1
    C

    class Solution {
    public:
    void oddEvenHelper(ListNode * root,ListNode * * last_odd){
    if(root==NULL || root->next==NULL)
    return;
    ListNode prev=root->next;
    if(root->next->next==NULL)
    ( * last_odd) = root;
    root->next=root->next->next;
    if(root->next && root->next->next==NULL)
    ( * last_odd) = root->next;
    oddEvenHelper(root->next,last_odd);
    prev->next = ( * last_odd)->next;
    ( * last_odd)->next = prev;
    }
    ListNode
    oddEvenList(ListNode* head) {
    ListNode* last_odd = NULL;
    oddEvenHelper(head,&last_odd);
    return head;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.