Skip to content

Commit cc24e3f

Browse files
Added solution
1 parent 27e8782 commit cc24e3f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
void reorderList(ListNode* head) {
12+
deque<ListNode*> d;
13+
if(head == NULL) return;
14+
ListNode *temp = head;
15+
while(temp != NULL){
16+
d.push_back(temp);
17+
temp = temp->next;
18+
}
19+
d.pop_front(); //since temp = head and we dont need to consider the head again..
20+
21+
while(d.size() > 0){
22+
23+
head->next = d.back();
24+
d.pop_back();
25+
head = head->next;
26+
27+
if(d.size() > 0){
28+
head->next = d.front();
29+
d.pop_front();
30+
head = head->next;
31+
}
32+
}
33+
head->next = NULL;
34+
35+
}
36+
};

0 commit comments

Comments
 (0)