1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public: ListNode* oddEvenList(ListNode* head) { auto evenHead = new ListNode(-1); auto oddHead = new ListNode(-1);
auto evenTail = evenHead, oddTail = oddHead;
int k = 1; while (head) { if (k & 0x1) { evenTail->next = head; evenTail = head; } else { oddTail->next = head; oddTail = head; } k++; head = head->next; } oddTail->next = NULL; evenTail->next = oddHead->next; return evenHead->next; } };
|