Skip to content

Commit ceef27d

Browse files
authored
Create Day16.cpp
1 parent 36c3295 commit ceef27d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Day16.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Author: Aryan Yadav
3+
Odd Even Linked List
4+
5+
Complexity:O(n)
6+
Algorithm: NA
7+
Difficulty: Medium
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* struct ListNode {
13+
* int val;
14+
* ListNode *next;
15+
* ListNode() : val(0), next(nullptr) {}
16+
* ListNode(int x) : val(x), next(nullptr) {}
17+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
18+
* };
19+
*/
20+
class Solution {
21+
public:
22+
ListNode* oddEvenList(ListNode* head) {
23+
if (head) {
24+
for (auto odd_tail = head, curr = head->next;
25+
curr && curr->next;
26+
curr = curr->next) {
27+
ListNode *even_head = odd_tail->next;
28+
odd_tail->next = curr->next;
29+
odd_tail = odd_tail->next;
30+
curr->next = odd_tail->next;
31+
odd_tail->next = even_head;
32+
}
33+
}
34+
return head;
35+
}
36+
};

0 commit comments

Comments
 (0)