forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReorder List.c
executable file
·44 lines (40 loc) · 1.01 KB
/
Reorder List.c
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
/*
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You must do this in-place without altering the nodes values.
*/
// Definition for singly-linked list.
struct ListNode {
int val;
struct ListNode *next;
};
void reorderList(struct ListNode* head) {
struct ListNode* stack[100001];
int top = 0;
struct ListNode *ptr = head, *tptr = NULL;
while(ptr) {
stack[top++] = ptr;
ptr = ptr->next;
}
ptr = head;
int fromStack = 1;
while (ptr) {
if (fromStack) {
tptr = ptr->next;
if (stack[top-1] == ptr)
break;
ptr->next = stack[--top];
fromStack = 0;
}
else {
if (tptr == ptr)
break;
ptr->next = tptr;
fromStack = 1;
}
ptr = ptr->next;
}
if (ptr)
ptr->next = NULL;
return head;
}