Odd Even Linked List LeetCode solution in C++

1
class Solution {
2
public:
3
ListNode *oddEvenList(ListNode *head) {
4
if (head == NULL || head->next == NULL || head->next->next == NULL) {
5
return head;
6
}
7
ListNode *first = head;
8
ListNode *second = head->next;
9
ListNode *last = head;
10
int count = 1;
11
while (last->next != NULL) {
12
last = last->next;
13
count++;
14
}
15

16
int n = count / 2;
17
while (n != 0) {
18
first->next = second->next;
19
last->next = second;
20
second->next = NULL;
21
first = first->next;
22
last = second;
23
second = first->next;
24
n--;
25
}
26
return head;
27
}
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0