奇数偶数序列

给定一串奇数偶数链表,将奇数排在前面,偶数排在后面。

Given 1->2->3->4->5->NULL,
return 1->3->5->2->4->NULL.

注:不是leetcode的那道Odd Even Linked List

ListNode* oddEvenList(ListNode* head) {
        if(head == NULL || head->next == NULL)
        return head;
        ListNode odd(0),  even(0);
        ListNode *p = head;
        ListNode *podd = &odd, *peven = &even;
        while(p!=NULL){
            if(p->val & 1 == 1){
                podd->next = p;
                podd = podd->next;
            }else{
                peven->next = p;
                peven = peven->next;
            }
            p = p->next;
        }
        peven->next = NULL;
        ListNode* temp = &even;
        temp = temp->next;
        podd->next = temp;
        temp = &odd;
        temp = temp->next;
        head = temp;
        return head;
    }


你可能感兴趣的:(LeetCode,C++,linklist)