Odd Even Linked List LeetCode solution in C++
1class Solution {2public:3ListNode *oddEvenList(ListNode *head) {4if (head == NULL || head->next == NULL || head->next->next == NULL) {5return head;6}7ListNode *first = head;8ListNode *second = head->next;9ListNode *last = head;10int count = 1;11while (last->next != NULL) {12last = last->next;13count++;14}1516int n = count / 2;17while (n != 0) {18first->next = second->next;19last->next = second;20second->next = NULL;21first = first->next;22last = second;23second = first->next;24n--;25}26return head;27}28};