Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 26, 2018 00:00
Show Gist options
  • Save kanrourou/cbe7fa2decd63c94ba3d55fb257f34cc to your computer and use it in GitHub Desktop.
Save kanrourou/cbe7fa2decd63c94ba3d55fb257f34cc to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(!head || !head->next)return head;
ListNode* oHead = head, *eHead = head->next, *oCurr = oHead, *eCurr = eHead;
while(eCurr->next)
{
oCurr->next = eCurr->next;
oCurr = oCurr->next;
if(!oCurr->next)break;
eCurr->next = oCurr->next;
eCurr = eCurr->next;
}
if(eCurr->next)eCurr->next = nullptr;
if(oCurr->next)oCurr->next = nullptr;
oCurr->next = eHead;
return oHead;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment