leetcode-328-奇偶链表

leetcode-328-奇偶链表

class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        if (head == NULL || head->next == NULL) return head;
        int cnt = 1;
        ListNode* mid = head;
        ListNode* curNode = head->next;
        while (curNode!= NULL && curNode->next != NULL) {
            ListNode *next = curNode->next;
            curNode->next = next->next;
            next->next = mid->next;
            mid->next = next;
            mid = next;
            curNode = curNode->next;
        }
        return head;
    }
};

int main()
{
    Solution sol;
    vector<int> input = {1,2,3,4,5,6,7,8};
    ListNode *head = createListNode(input);
    printLinkedList(sol.oddEvenList(head));
    return 0;
}