Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- ListNode* oddEvenList(ListNode* head) {
- if (!head || !head->next) {
- return head;
- }
- ListNode* odd = head;
- ListNode* even = head->next;
- ListNode* evenHead = even;
- while (even != nullptr && even->next != nullptr) {
- odd->next = even->next;
- odd = odd->next;
- even->next = odd->next;
- even = even->next;
- }
- odd->next = evenHead;
- return head;
- }
- };
- In this implementation, the `oddEvenList` function rearranges the linked list by separating the odd-indexed nodes from the even-indexed nodes and then merging them back together. The function returns the head of the modified linked list.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement