Simple C++ solution O(n)


  • 0
    K
    class Solution {
    public:
        ListNode* oddEvenList(ListNode* head) {
            
            if(head){
                ListNode* p = head;
                ListNode* q = p->next;
                ListNode* temp;
                
                while(q && q->next) {
                    temp = p->next;
                    p->next = q->next;
                    p = p->next;
                    
                    q->next = p->next;
                    p->next = temp;
                    q = q->next;
                }
            }
            return head;
        }
    };

Log in to reply
 

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