Skip to content

Commit

Permalink
Time: 29 ms (34.36%), Space: 10.6 MB (29.12%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed Dec 6, 2022
1 parent 13342e4 commit b2596bb
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 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,35 @@
/**
* 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) return NULL;
if (head->next == NULL) return head;
ListNode* even=head->next;
ListNode* temp=head->next;
ListNode* odd=head;
while(odd->next!=NULL && odd->next->next!=NULL)
{
odd->next=odd->next->next;
odd=odd->next;
if(even->next != NULL && even->next->next != NULL)
{
even->next=even->next->next;
even=even->next;
}

}
odd->next=temp;
even->next=NULL;
return head;

}
};

0 comments on commit b2596bb

Please sign in to comment.