LeetCode-Solutions-in-Cpp17

Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.


Project maintained by downdemo Hosted on GitHub Pages — Theme by mattgraham
class Solution {
 public:
  ListNode* oddEvenList(ListNode* head) {
    if (!head || !head->next) {
      return head;
    }
    ListNode* odd = head;
    ListNode* even = head->next;
    ListNode* evenStart = head->next;
    while (odd->next && odd->next->next) {
      odd->next = even->next;
      odd = odd->next;
      even->next = odd->next;
      even = even->next;
    }
    if (even) {
      even->next = nullptr;
    }
    odd->next = evenStart;
    return head;
  }
};