lurenaa的博客

😍双指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(!head || !head->next)
return head;
ListNode* head2 = head->next, *h2 = head2;
ListNode* iter = head2->next, *h1 = head;
bool isEven = true;
while(iter) {
if(isEven) {
h1->next = iter;
h1 = h1->next;
} else {
h2->next = iter;
h2 = h2->next;
}
isEven = !isEven;
iter = iter->next;
}
h1->next = head2;
h2->next = nullptr;
return head;
}
};

Accepted

71/71 cases passed (8 ms)

Your runtime beats 98.43 % of cpp submissions

Your memory usage beats 17.56 % of cpp submissions (9.7 MB)