-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path143_reorderList.cpp
More file actions
39 lines (39 loc) · 914 Bytes
/
143_reorderList.cpp
File metadata and controls
39 lines (39 loc) · 914 Bytes
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
if(head==NULL||head->next == NULL)
return;
ListNode *p = head,*q = head->next;
while(q->next!=NULL){
p = p->next;
q = q->next;
if(q->next == NULL)
break;
q = q->next;
}
ListNode *tmp;
while(p->next!=q){
tmp = p->next;
p->next= p->next->next;
tmp->next = q->next;
q->next = tmp;
}
p->next = NULL;
p = head;
while(q!=NULL){
tmp = q->next;
q->next = p->next;
p->next = q;
q = tmp;
p = p->next->next;
}
}
};