cbangchen
11/11/2018 - 7:29 AM

328. Odd Even Linked List - DifficultyMedium - 2018.8.7

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        if (head == NULL) {
            return head;
        }
        ListNode *oddNode = head;
        ListNode *evenNode = head->next;
        ListNode *evenhead = head->next;
        while (evenNode && evenNode->next) {
            oddNode->next = evenNode->next;
            evenNode->next = evenNode->next->next;
            oddNode = oddNode->next;
            evenNode = evenNode->next;
        }
        oddNode->next = evenhead;
        return head;
    }
};