nikunjsoni

328

Mar 17th, 2021
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Definition for singly-linked list.
  3.  * struct ListNode {
  4.  *     int val;
  5.  *     ListNode *next;
  6.  *     ListNode() : val(0), next(nullptr) {}
  7.  *     ListNode(int x) : val(x), next(nullptr) {}
  8.  *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9.  * };
  10.  */
  11. class Solution {
  12. public:
  13.     ListNode* oddEvenList(ListNode* head) {
  14.         ListNode *even, *odd, *even_head;
  15.         if(!head) return head;
  16.         odd = head;
  17.         even = odd->next;
  18.         even_head = even;
  19.         while(even && even->next){
  20.             odd->next = even->next;
  21.             odd = odd->next;
  22.             even->next = odd->next;
  23.             even = even->next;
  24.         }
  25.         odd->next = even_head;
  26.         return head;
  27.     }
  28. };
RAW Paste Data