-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0143.cpp
More file actions
53 lines (51 loc) · 1.52 KB
/
Copy path0143.cpp
File metadata and controls
53 lines (51 loc) · 1.52 KB
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
/**
* 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 {
private:
int evaluateListLength(ListNode* head) {
return head ? 1 + evaluateListLength(head->next) : 0;
}
ListNode* reverseList(ListNode* head) {
ListNode* current = head, *previous = nullptr, *next = nullptr;
while (current) {
next = current->next;
current->next = previous;
previous = current;
current = next;
}
return previous;
}
pair<ListNode*,ListNode*> splitLists(ListNode* head) {
int n = evaluateListLength(head);
int steps = n / 2;
ListNode* current = head;
for (int i = 1; i <= steps; i++) {
current = current->next;
}
ListNode* l1 = head;
ListNode* l2 = reverseList(current->next);
current->next = nullptr;
return {l1, l2};
}
public:
void reorderList(ListNode* head) {
pair<ListNode*,ListNode*> lists = splitLists(head); // original first half, reversed second half
ListNode* l1 = lists.first, *l2 = lists.second, *next1, *next2;
while (l1 && l2) {
next1 = l1->next;
l1->next = l2;
next2 = l2->next;
l2->next = next1;
l1 = next1;
l2 = next2;
}
}
};