We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 27e8782 commit cc24e3fCopy full SHA for cc24e3f
reorder_linkedList_alternate_deque.cpp
@@ -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
31
32
33
+ head->next = NULL;
34
35
36
+};
0 commit comments