This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
}; |
No comments:
Post a Comment