Simple 13ms C++ solution


  • 0
    P
    ListNode* oddEvenList(ListNode* head) {
        if (head == NULL || head->next == NULL)
            return head;
        ListNode *odd = head, *even = head->next;
        ListNode *curr = head->next->next, *evenHead = even;
        int i = 1;
        while(curr != NULL)
        {
            if (i%2 == 1)
            {
                odd->next = curr;
                odd = odd->next;
            }
            else
            {
                even->next = curr;
                even = even->next;
            }
            curr = curr->next;
            i++;
        }
        odd->next = evenHead;
        even->next = NULL;
        return head;
    }

Log in to reply
 

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