Advertisement
LikeRampage

C++ leetcode 328. Odd Even Linked List CHATGPT UNLIMITED

Apr 20th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. class Solution {
  2. public:
  3. ListNode* oddEvenList(ListNode* head) {
  4. if (!head || !head->next) {
  5. return head;
  6. }
  7.  
  8. ListNode* odd = head;
  9. ListNode* even = head->next;
  10. ListNode* evenHead = even;
  11.  
  12. while (even != nullptr && even->next != nullptr) {
  13. odd->next = even->next;
  14. odd = odd->next;
  15. even->next = odd->next;
  16. even = even->next;
  17. }
  18.  
  19. odd->next = evenHead;
  20.  
  21. return head;
  22. }
  23. };
  24.  
  25.  
  26. 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.
Tags: C++ chatGPT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement