-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodd-even-linked-list.cpp
55 lines (46 loc) · 1.2 KB
/
odd-even-linked-list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* Copyright (c) 2018 Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
#include <cstddef>
using namespace std;
class Solution {
public:
// https://leetcode.com/problems/odd-even-linked-list/
ListNode *oddEvenList(ListNode *head) {
// Assumptions:
// - 1-indexed, so the head pointer is '1'
//
// Observations:
// - maintain e.g. a bool to indicate whether we are serving even or odd
// - maintain a pointer to first / last element in odd / even
// - makes appending O(1) and lets us keep a reference for
// concatenating at the end
// - visit each node exactly once, O( N ) in time and O( 1 ) in space
if (nullptr == head) {
return nullptr;
}
enum {
ODD,
EVEN,
};
ListNode *m_head[2]{};
ListNode *m_tail[2]{};
ListNode *it = head;
bool odd = ODD;
for (; it; it = it->next, odd ^= 1) {
if (nullptr == m_head[odd]) {
m_head[odd] = m_tail[odd] = it;
} else {
m_tail[odd]->next = it;
m_tail[odd] = m_tail[odd]->next;
}
}
m_tail[ODD]->next = m_head[EVEN];
if (nullptr != m_tail[EVEN]) {
m_tail[EVEN]->next = nullptr;
}
return m_head[ODD];
}
};