2024每日刷题(153)
Leetcode—328. 奇偶链表
实现代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
ListNode oddNode(0);
ListNode evenNode(0);
ListNode* odd = &oddNode;
ListNode* even = &evenNode;
for(int cnt = 0; head; head = head->next) {
if(cnt ^= 1) {
odd->next = head;
odd = odd->next;
} else {
even->next = head;
even = even->next;
}
}
odd->next = evenNode.next;
even->next = nullptr;
return oddNode.next;
}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!