Python one-pass, O(1) memory, simple code, beats 90%
\nclass Solution:\n    def sortArrayByParityII(self, a):\n        i = 0 # pointer for even misplaced\n        j = 1 # pointer for odd misplaced\n        sz = le
```\nclass Solution:\n    def sortArrayByParityII(self, a):\n        i = 0 # pointer for even misplaced\n        j = 1 # pointer for odd misplaced\n        sz = len(a)\n        \n        # invariant: for every misplaced odd there is misplaced even\n        # since there is just enough space for odds and evens\n\n        while i < sz and j < sz:\n            if a[i] % 2 == 0:\n                i += 2\n            elif a[j] % 2 == 1:\n                j += 2\n            else:\n                # a[i] % 2 == 1 AND a[j] % 2 == 0\n                a[i],a[j] = a[j],a[i]\n                i += 2\n                j += 2\n\n        return a\n```
[]