-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReorder_List.cpp
54 lines (49 loc) · 1.47 KB
/
Reorder_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
class Solution {
public:
ListNode *reverseList (ListNode *head) {
if (head == NULL || head->next == NULL)
return head;
ListNode *new_head = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return new_head;
}
void reorderList(ListNode *head) {
if (head == NULL || head->next == NULL || head->next->next == NULL)
return;
/* Get list's size */
int size = 0;
ListNode *cur = head;
do {
++size;
cur = cur->next;
} while(cur);
/* Slpit the original list into two halves */
int half = size / 2;
cur = head;
int ct = 0;
do {
cur = cur->next;
} while (++ct < half - 1);
ListNode *second_list = reverseList(cur->next);
cur->next = NULL;
/* Merge */
ListNode *first_list = head->next;
cur = head;
cur->next = second_list;
second_list = second_list->next;
cur = cur->next;
while (first_list && second_list) {
cur->next = first_list;
first_list = first_list->next;
cur = cur->next;
cur->next = second_list;
second_list = second_list->next;
cur = cur->next;
}
if (first_list)
cur->next = first_list;
else if (second_list)
cur->next = second_list;
}
};