Skip to content

Commit

Permalink
Time: 8 ms (59.80%), Space: 15.2 MB (18.53%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakkumar9546 committed Jul 24, 2024
1 parent a52982e commit edb3b08
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 0328-odd-even-linked-list/0328-odd-even-linked-list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(head==NULL || head->next==NULL){
return head;
}
ListNode* odd = head;
ListNode* even = head->next;
ListNode* evenHead = even;
while (even != NULL && even->next != NULL) {
odd->next = odd->next->next;
even->next = even->next->next;
odd = odd->next;
even = even->next;
}
odd->next = evenHead;
return head;
}
};

0 comments on commit edb3b08

Please sign in to comment.