
代码:
class Solution328_medium {
public:
ListNode* oddEvenList(ListNode* head) {
if (nullptr == head || nullptr == head->next)
return head;
ListNode* odd_head = head, * odd = head;
ListNode* even_head = head->next, * even = head->next;
while (nullptr != odd && nullptr != even) {
odd->next = even->next;
odd = odd->next;
if (nullptr != odd)
even->next = odd->next;
even = even->next;
}
odd = odd_head;
while (nullptr != odd->next)
odd = odd->next;
odd->next = even_head;
return odd_head;
}
};

该博客主要讨论了一种算法问题,即如何将一个链表重新排列,使得奇数索引位置的节点链接在一起,偶数索引位置的节点链接在一起,最后再将奇数链表和偶数链表连接起来。提供的C++代码实现了一个解决方案,通过两个指针遍历链表,分别处理奇数和偶数位置的节点,最后将两个链表连接成新的有序链表。

被折叠的 条评论
为什么被折叠?



