id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n deque<int> vals;\n ListNode *curr = head;\n while (curr != nullptr) {\n vals.push_back(curr->val);\n curr = curr->next;\n }\n\n curr = head;\n while (!vals.empty()) {\n curr->val = vals.front();\n vals.pop_front();\n curr = curr->next;\n if (!vals.empty()) {\n curr->val = vals.back();\n vals.pop_back();\n curr = curr->next;\n }\n }\n }\n};",
"memory": "23300"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n \n stack<ListNode *> s;\n ListNode *cur = head;\n \n while(cur) {\n s.push(cur);\n cur = cur->next;\n }\n \n int len = s.size()/2;\n cur = head;\n \n for(int i=0; i<len; i++) {\n \n ListNode *next = cur->next;\n \n cur->next = s.top(); \n s.top()->next = next; s.pop(); \n \n cur = next;\n }\n \n if(cur)\n cur->next = NULL;\n }\n};\n",
"memory": "23300"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n ListNode* reverseList(ListNode* head) {\n if(head==NULL || head->next == NULL) return head;\n ListNode* newHead = reverseList(head->next);\n head->next->next = head;\n head->next = NULL;\n return newHead;\n }\n void reorderList(ListNode* head) {\n ListNode* slow= head;\n ListNode* fast = head;\n while(fast->next!= NULL && fast->next->next!=NULL){\n slow = slow->next;\n fast=fast->next->next;\n }\n ListNode* b = reverseList(slow->next);\n ListNode* a= head;\n slow->next=NULL;\n // MEREGE ALTERNATIVELY\n ListNode* c= new ListNode(100);\n ListNode* tempc=c;\n ListNode* tempa = a;\n ListNode* tempb= b;\n while(tempa && tempb){\n tempc->next = tempa;\n tempa =tempa->next;\n tempc = tempc->next;\n\n tempc->next = tempb;\n tempb =tempb->next;\n tempc = tempc->next;\n }\n tempc->next = tempa;\n head = c->next;\n\n\n }\n};",
"memory": "23400"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n deque<int> vals;\n ListNode *curr = head;\n while (curr != nullptr) {\n vals.push_back(curr->val);\n curr = curr->next;\n }\n\n curr = head;\n while (!vals.empty()) {\n curr->val = vals.front();\n vals.pop_front();\n curr = curr->next;\n if (!vals.empty()) {\n curr->val = vals.back();\n vals.pop_back();\n curr = curr->next;\n }\n }\n }\n};",
"memory": "23400"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n if(!head || !head->next || !head->next->next)\n {\n return;\n }\n stack<ListNode*> st;\n ListNode* cur=head;\n int n=0;\n while(cur)\n {\n n++;\n st.push(cur);\n cur=cur->next;\n }\n cur=head;\n for(int i=0;i<n/2;i++)\n {\n ListNode* node=st.top();\n st.pop();\n node->next=cur->next;\n cur->next=node;\n cur=cur->next->next;\n }\n cur->next=NULL;\n }\n};",
"memory": "23500"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n stack<ListNode*> S;\n ListNode* curr = head;\n while(curr){\n S.push(curr);\n curr = curr->next;\n }\n curr = head;\n int n = S.size();\n if(n<=2) return;\n ListNode* next;\n for(int i =0; i<n/2; i++){\n next = curr->next;\n curr->next = S.top();\n S.pop();\n curr = curr->next;\n curr->next = next;\n curr = curr->next;\n }\n curr->next = nullptr;\n }\n};",
"memory": "23500"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nvoid printList(const ListNode *n){\n cout<<n->val<<\" \";\n }\nvoid printQ(deque<ListNode *> &Q){\n for_each(Q.begin(), Q.end(), printList);\n cout<<endl;\n }\nclass Solution {\n deque<ListNode *> Q;\n void traverse(ListNode * head){\n while(head){\n Q.push_back(head);\n head=head->next;\n \n }\n }\n ListNode * createListFromQ(){\n ListNode * head = new ListNode(0);\n ListNode * sentinel_head = head;\n while (!Q.empty()){\n if (Q.size()>1){\n auto first=Q.front();\n auto second=Q.back();\n cout<<first->val<<\" \"<<second->val<<endl;\n \n head->next=first;\n head=head->next;\n head->next=second;\n head=head->next;\n head->next=nullptr;\n Q.pop_front();\n Q.pop_back();\n\n }\n else{\n auto first=Q.front();\n cout<<first->val<<endl;\n head->next=first;\n head=head->next;\n head->next=nullptr;\n Q.pop_front();\n }\n }\n return sentinel_head->next;\n }\n\n \npublic:\n void reorderList(ListNode* head) {\n if (!head){\n head=nullptr;\n return;\n } \n traverse(head);\n printQ(Q);\n head=createListFromQ();\n }\n};",
"memory": "23600"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) \n {\n stack<ListNode*> st;\n\n ListNode* curr = head;\n\n while(curr!=NULL){\n st.push(curr);\n curr = curr -> next;\n } \n\n int k = st.size() / 2;\n curr = head;\n while(k--){\n ListNode* topnode = st.top();\n st.pop();\n\n ListNode* temp = curr -> next;\n curr -> next = topnode;\n topnode -> next = temp;\n curr = temp;\n }\n curr -> next = NULL;\n }\n};",
"memory": "23600"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n ListNode* last = head, *start = head, *prev = NULL;\n if(head == NULL || head->next == NULL)\n {\n return;\n }\n\n while(last->next != NULL)\n {\n prev = last;\n last = last->next;\n }\n\n prev->next = NULL;\n reorderList(start->next);\n last->next = start->next;\n start->next = last;\n head = start;\n }\n};",
"memory": "23700"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n vector<ListNode *> arr;\n ListNode *ptr = head;\n while(ptr){\n arr.push_back(ptr);\n ptr = ptr -> next;\n }\n int left = 0, right = arr.size()-1;\n while(left<right){\n arr[left] -> next = arr[right];\n left++;\n if(left == right) break;\n arr[right] -> next = arr[left];\n right--;\n }\n arr[left] -> next = NULL;\n }\n};",
"memory": "23800"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\n public:\n void reorderList(ListNode* head) {\n ListNode* it = head;\n bool end = false;\n r(it, head, end);\n }\n\n void r(ListNode*& it, ListNode* node, bool& end) {\n if (node->next == nullptr) {\n cout << node->val << '\\n';\n return;\n } else { \n r(it, node->next, end);\n if (end) {\n return;\n }\n ListNode* btw = node->next;\n if (it->next == btw || it->next == nullptr) {\n end = true;\n return;\n }\n ListNode* it2 = it->next;\n\n it->next = btw;\n btw->next = it2;\n node->next = nullptr;\n it = it->next->next;\n }\n }\n};",
"memory": "24500"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n//method 1- using vectors, feed value in vectors acheive required order and then make listnode out of it\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n ListNode* temp=head;\n vector<int> vect1;\n while(temp){\n vect1.push_back(temp->val);\n temp=temp->next;\n \n }\n vector<int> vect2;\n for(int i=vect1.size()-1;i>=0;i--){\n vect2.push_back(vect1[i]);\n }\n vector<int> vect;\n int n=vect1.size();\n int k=n/2;\n for(int j=0;j<k;j++){\n vect.push_back(vect1[j]);\n vect.push_back(vect2[j]);\n }\n if(n%2==1){vect.push_back(vect1[k]);}\n temp=head;\n for(int i=0;i<n;i++){\n temp->val=vect[i];\n temp=temp->next;\n }\n \n\n }\n};",
"memory": "24600"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n ListNode *ptr = head;\n bool flag = false;\n reorderList(ptr, head, flag);\n }\n\nprivate:\n void reorderList(ListNode *&sptr, ListNode *eptr, bool &flag) {\n if(eptr == nullptr)\n return;\n\n reorderList(sptr, eptr->next, flag);\n\n if(flag) {\n return;\n }\n\n if(sptr == eptr || sptr->next == eptr) {\n eptr->next = nullptr;\n flag = true;\n return;\n }\n\n ListNode *temp = sptr->next;\n sptr->next = eptr;\n sptr = temp;\n eptr->next = sptr;\n }\n};",
"memory": "24700"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\nprivate:\n ListNode* left;\n int n = 0;\npublic:\n void reverse(ListNode* right, int cnt) {\n if(!right) return;\n n = max(n,cnt+1);\n reverse(right->next, cnt+1);\n if(cnt <= n/2) return;\n ListNode* node = left->next;\n left->next = right;\n right->next = node;\n left = node;\n }\n\n void reorderList(ListNode* head) {\n \n if(!head || !head->next ||!head->next->next) return;\n\n left = head;\n reverse(left->next, 2);\n left->next = NULL;\n\n }\n};\n\n// 1 2 3 4 5 6 7 8\n\n// 2,2\n// n = 3\n// 3,3\n// n = 4\n// 4,4\n// n=5\n// 5,5\n// n=6\n// 6,6\n// n=7\n// 7,7\n// n=8\n// 8,8\n// 1->8->2",
"memory": "24800"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void reorderList(ListNode* head) {\n if (!head) return;\n\n ListNode* p1 = head;\n int c = 0;\n vector<pair<int, ListNode*>> mp;\n\n // Store the nodes in a vector\n while (p1 != NULL) {\n mp.push_back({p1->val, p1});\n p1 = p1->next;\n c++;\n }\n\n // Reorder the list\n int i = 0, j = c - 1;\n p1 = head;\n\n while (i < j) {\n ListNode* p2 = mp[i].second->next; // Save the next node\n mp[i].second->next = mp[j].second; // Point to the node from the end\n mp[j].second->next = p2; // Point the node from end to next node of the ith node\n i++;\n j--;\n }\n\n mp[i].second->next = NULL;\n }\n};\n",
"memory": "24900"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* left;\n int n=0;\n void helper(ListNode* right, int cnt){\n if(!right) return;\n n=max(n,cnt+1);\n helper(right->next,cnt+1);\n\n if(cnt<=n/2) return;\n\n ListNode* Next = left->next;\n left->next = right;\n right->next = Next;\n left=Next;\n }\n void reorderList(ListNode* head) {\n if(!head || !head->next || !head->next->next) return;\n left=head;\n helper(head->next,2);\n left->next=nullptr;\n }\n};",
"memory": "25000"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n vector<ListNode*> items, res;\n while(head){\n items.push_back(head);\n head = head->next;\n }\n \n int left = 0, right = items.size()-1;\n bool toLeft = true;\n while(left <= right){\n if(toLeft == true) \n {\n res.push_back(items[left]);\n left++;\n }\n else{\n res.push_back(items[right]);\n right--;\n }\n toLeft = !toLeft;\n }\n // for(auto it: res) cout<<it->val<<\", \";\n for(int i = 1; i < items.size(); i++){\n res[i-1]->next = res[i];\n if(i == items.size()-1) res[i]->next = nullptr;\n }\n \n }\n};",
"memory": "25100"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n if(!head->next) return ;\n vector<int>ans;\n ListNode*temp=head;\n while(temp!=nullptr){\n ans.push_back(temp->val);\n temp=temp->next;\n }\n\n int n=ans.size();\n\n ListNode*dummy=head;\n head->next=nullptr;\n dummy->next=new ListNode(ans[n-1]);\n dummy=dummy->next;\n for(int i=1,j=n-2;i<j;i++,j--){\n dummy->next=new ListNode(ans[i]);\n dummy=dummy->next;\n dummy->next=new ListNode(ans[j]);\n dummy=dummy->next;\n }\n if(ans.size()%2!=0){\n dummy->next=new ListNode(ans[n/2]);\n }\n }\n};",
"memory": "25200"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "\n\n/*\n\nMY FIRST INTUTION\nhere i didnot read the question properly and tried to change value insted of nodes\n\n*/\nclass Solution {\npublic:\n \n void getorder(ListNode* head,vector<int>& v)\n {\n while(head!=NULL)\n {\n v.push_back(head->val);\n head=head->next;\n }\n }\n \n \n void display(ListNode* head)\n {\n while(head!=NULL)\n {\n cout<<head->val<<\" \";\n head=head->next;\n }\n \n cout<<endl;\n }\n \n \n \n \n void reorderList(ListNode*& head) {\n \n vector<int> v;\n getorder(head,v);\n \n \n int i=0;\n int j = v.size()-1;\n ListNode* current = new ListNode(-1);\n ListNode* c = NULL;\n \n current->next=c;\n \n while(j>i)\n {\n //cout<<\"i entered here for i and j \"<<i<<\" \"<<j<<endl;\n \n ListNode* a = new ListNode(v[i]);\n ListNode* b = new ListNode(v[j]);\n \n a->next=b;\n \n if(c==NULL)\n {\n //cout<<\"i entered here\\n\";\n current=a;\n c=b;\n \n }else{\n \n //cout<<\"mai aab yaha aaya\\n\";\n c->next=a;\n c=b;\n }\n \n i++;\n j--;\n \n \n \n \n }\n \n if(i==j)\n {\n //cout<<\"idher aana nahi pada\\n\";\n ListNode* a = new ListNode(v[j]);\n if(c==NULL)\n {\n current=a;\n }else{\n c->next=a;\n }\n \n }\n \n \n head->next=current->next;\n //head=head->next;\n //current;\n \n \n // ListNode* p =cuuernt;\n \n //display(head);\n \n \n \n }\n};\n\n\n",
"memory": "25300"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n if(head == NULL || head->next == NULL)\n return;\n \n vector<int> temp;\n ListNode* curr=head;\n while(curr!=NULL)\n {\n temp.push_back(curr->val);\n curr = curr->next;\n }\n ListNode* nhead=NULL;\n ListNode* ntail=NULL;\n for(int i=0;i<temp.size()/2; i++)\n {\n ListNode* curr=new ListNode(temp[i]);\n if(nhead == NULL)\n {\n nhead = curr;\n ntail = curr;\n }\n else\n {\n ntail->next = curr;\n ntail=ntail->next;\n }\n\n ListNode* curr1=new ListNode(temp[temp.size()-i-1]);\n ntail->next = curr1;\n ntail=ntail->next;\n\n }\n if (temp.size() % 2 != 0) {\n ListNode* newNode = new ListNode(temp[temp.size()/2]);\n ntail->next = newNode;\n ntail = ntail->next;\n }\n ntail->next = NULL;\n\n *head = *nhead;\n\n }\n};",
"memory": "25300"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n \n stack<int> CollectTheDataInStack(ListNode* head)\n {\n ListNode* temp = head;\n stack<int> st;\n while(temp != nullptr)\n {\n st.push(temp->val);\n temp = temp->next;\n }\n\n return st;\n }\n\n void insert(ListNode* head, int val)\n {\n ListNode* newNode = new ListNode(val);\n\n if (head->val == -10)// empty\n {\n head->val = val;\n }\n else if (head->next == nullptr)\n {\n head->next = newNode;\n }\n else\n {\n ListNode* temp = head;\n\n while (temp->next != nullptr)\n temp = temp->next;\n\n temp->next = newNode;\n }\n }\n\n void reorderList(ListNode*& head) {\n \n ListNode* temp = head;\n ListNode* NewList = new ListNode(-10);\n\n stack<int> st = CollectTheDataInStack(head);\n int size = st.size();\n int idx = 1;\n\n while(size != 0)\n {\n if(idx % 2 == 1)\n {\n insert(NewList, temp->val);\n temp = temp->next;\n }\n else\n {\n insert(NewList, st.top());\n st.pop();\n }\n\n idx++;\n size--;\n }\n\n temp = head;\n while(temp != nullptr)\n {\n ListNode* temp2 = NewList;\n\n temp->val = NewList->val;\n\n temp = temp->next;\n\n NewList = NewList->next;\n delete temp2;\n }\n }\n};",
"memory": "25400"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n \n stack<int> CollectTheDataInStack(ListNode* head)\n {\n ListNode* temp = head;\n stack<int> st;\n while(temp != nullptr)\n {\n st.push(temp->val);\n temp = temp->next;\n }\n\n return st;\n }\n\n void insert(ListNode* head, int val)\n {\n ListNode* newNode = new ListNode(val);\n\n if (head->val == 0)// empty\n {\n head->val = val;\n }\n else if (head->next == nullptr)\n {\n head->next = newNode;\n }\n else\n {\n ListNode* temp = head;\n\n while (temp->next != nullptr)\n temp = temp->next;\n\n temp->next = newNode;\n }\n }\n\n void reorderList(ListNode*& head) {\n \n ListNode* temp = head;\n ListNode* NewList = new ListNode();\n\n stack<int> st = CollectTheDataInStack(head);\n int size = st.size();\n int idx = 1;\n\n while(size != 0)\n {\n if(idx % 2 == 1)\n {\n insert(NewList, temp->val);\n temp = temp->next;\n }\n else\n {\n insert(NewList, st.top());\n st.pop();\n }\n\n idx++;\n size--;\n }\n\n temp = head;\n while(temp != nullptr)\n {\n ListNode* temp2 = NewList;\n\n temp->val = NewList->val;\n\n temp = temp->next;\n\n NewList = NewList->next;\n delete temp2;\n }\n }\n};",
"memory": "25500"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void helper(ListNode* head, vector<int>& normalList, vector<int>& reverseList) {\n if (head == NULL) return;\n\n normalList.push_back(head->val);\n\n helper(head->next, normalList, reverseList);\n\n reverseList.push_back(head->val);\n }\n void reorderList(ListNode* head) {\n vector<int> normalList;\n vector<int> reverseList;\n\n helper(head, normalList, reverseList);\n\n int i = 0, j = 0;\n int n = 0;\n ListNode* temp = head;\n\n while (n < normalList.size()) {\n if (n%2 == 0) temp->val = normalList[i], i++;\n else temp->val = reverseList[j], j++;\n n++;\n temp = temp->next;\n }\n\n }\n};",
"memory": "25600"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n ListNode* revHead;\n ListNode* tempHead = head;\n int swaps = 0, n = 0;\n while(tempHead){\n ListNode* temp = new ListNode(tempHead->val);\n n++;\n if(n == 1)\n revHead = temp;\n else\n temp->next = revHead;\n revHead = temp;\n tempHead = tempHead->next;\n }\n swaps = (n % 2) ? (n/2) : ((n/2) - 1);\n while(head && swaps){\n ListNode* temp = new ListNode(revHead->val, head->next);\n head->next = temp;\n swaps--;\n revHead = revHead->next;\n head = head->next->next;\n }\n if(n%2) head->next = nullptr;\n else head->next->next = nullptr;\n }\n};",
"memory": "25700"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n vector<ListNode*> all;\n auto curr = head;\n while(curr) {\n all.push_back(curr);\n curr= curr->next;\n }\n int idx = 1;\n bool last = 1;\n head->next = nullptr;\n curr = head;\n while(curr && idx < all.size()) {\n if(last) {\n ListNode* nxt = new ListNode(all.back()->val);\n curr->next = nxt;\n all.pop_back();\n }\n else {\n ListNode* nxt = new ListNode(all[idx++]->val);\n curr->next = nxt;\n } \n curr = curr->next;\n last = !last;\n }\n }\n};",
"memory": "25800"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\n\n#include <tuple>\n\nclass Solution {\npublic:\n std::tuple<ListNode*, ListNode*> helper(ListNode* slow, ListNode* fast) {\n if (fast == nullptr || fast->next == nullptr) {\n return {slow, fast == nullptr ? slow : slow->next};\n }\n\n auto [slow_ret, fast_ret] = helper(slow->next, fast->next->next);\n ListNode* next = fast_ret->next;\n\n if (fast_ret->next != nullptr) fast_ret->next = fast_ret->next->next;\n\n slow->next = next;\n if (next != nullptr) next->next = slow_ret;\n\n return {slow, fast_ret};\n }\n\n void reorderList(ListNode* head) { \n helper(head, head->next); \n }\n};",
"memory": "25900"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n vector<int> ele;\n ListNode * temp = head;\n while(temp != nullptr){\n ele.push_back(temp->val);\n temp = temp->next;\n }\n int i = 0;\n int j = ele.size()-1;\n vector<int> ele2;\n while(i<=j){\n if(i == j){\n ele2.push_back(ele[i]);\n break;\n }\n ele2.push_back(ele[i]);\n ele2.push_back(ele[j]);\n i++;\n j--;\n }\n\n head->next = nullptr;\n head->val = ele2[0];\n temp= head;\n for(int i = 1;i<ele2.size();i++){\n temp->next = new ListNode(ele2[i]);\n temp =temp->next;\n }\n }\n};",
"memory": "25900"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n if (!head || !head->next) return;\n\n std::list<ListNode*> tails = {head};\n ListNode* tail = head;\n size_t size = 0;\n while ((tail->next) != nullptr) {\n tail = tail->next; \n tails.push_back(tail);\n size++;\n }\n tails.pop_back();\n\n ListNode* headNext;\n while (tails.size() > size / 2) {\n std::cout << \"head \" << head->val << \" tail \" << tail->val << std::endl;\n headNext = head->next;\n head->next = tail;\n tail->next = headNext;\n\n head = headNext;\n tail = tails.back(); tails.pop_back();\n }\n head->next = nullptr;\n std::cout << \"done\";\n }\n};",
"memory": "26000"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n list<ListNode*> nodeList;\n ListNode* trackingHead = NULL;\n while(head != NULL)\n {\n nodeList.push_back(head);\n head = head->next;\n }\n\n\n while(!nodeList.empty())\n {\n cout << \" huh\";\n ListNode* firstNode;\n ListNode* nextNode = NULL;\n\n firstNode = nodeList.front();\n nodeList.pop_front();\n cout << \" first:\" << firstNode->val;\n\n if(!nodeList.empty())\n {\n nextNode = nodeList.back();\n nodeList.pop_back(); \n cout << \" next:\" << nextNode->val;\n\n }\n if(trackingHead != NULL) trackingHead->next = firstNode;\n firstNode->next = nextNode;\n if(nextNode != NULL) \n {\n trackingHead = nextNode;\n nextNode->next = NULL;\n }\n cout << \" huh\";\n\n }\n\n cout << \"are you fucking me>?\";\n }\n};",
"memory": "26100"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n ListNode* dummy=head;\n ListNode* curr=head;\n vector<int>v;\n while(dummy!=NULL){\n v.push_back(dummy->val);\n dummy=dummy->next;\n }\n vector<int>vcopy(v);\n int total=0;\n int n=v.size();\n vector<int>fv;\n reverse(vcopy.begin(),vcopy.end());\n for(int i=0;i<n;i++){\n fv.push_back(v[i]);\n total++;\n if(total==n){\n break;\n }\n fv.push_back(vcopy[i]);\n total++;\n if(total==n){\n break;\n }\n }\n ListNode* dnode=head;\n for(int i=1;i<n;i++){\n ListNode* temp=new ListNode(fv[i]);\n dnode->next=temp;\n dnode=dnode->next;\n }\n\n }\n};",
"memory": "26200"
} |
143 | <p>You are given the head of a singly linked-list. The list can be represented as:</p>
<pre>
L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>
</pre>
<p><em>Reorder the list to be on the following form:</em></p>
<pre>
L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …
</pre>
<p>You may not modify the values in the list's nodes. Only nodes themselves may be changed.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4]
<strong>Output:</strong> [1,4,2,3]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [1,2,3,4,5]
<strong>Output:</strong> [1,5,2,4,3]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>1 <= Node.val <= 1000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n void reorderList(ListNode* head) {\n \n vector<ListNode*> temp;\n\n ListNode* curr = head;\n\n while(curr!=NULL)\n {\n temp.push_back(curr);\n\n curr = curr->next;\n }\n\n // for(auto i:temp) cout<<i->val<<\" \";\n\n vector<ListNode*> vec;\n\n int i =0;\n int j = temp.size()-1;\n\n while(i<=j)\n {\n if(i==j)\n {\n vec.push_back(temp[i]);\n\n break;\n }\n \n vec.push_back(temp[i]);\n vec.push_back(temp[j]);\n\n i++;\n j--;\n }\n\n head -> next = NULL;\n\n curr = head;\n\n for(int i=1;i<vec.size();i++)\n {\n ListNode* t = new ListNode(vec[i]->val);\n\n curr -> next = t;\n\n curr = curr -> next;\n }\n }\n};",
"memory": "26900"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* findRightMost(TreeNode* node)\n {\n while(node->right)\n node = node->right;\n return node;\n }\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> res;\n TreeNode* current = root;\n while(current)\n {\n cout << current->val << endl;\n res.push_back(current->val);\n if(!current->left)\n {\n current = current->right;\n } else{\n TreeNode* rightMostOfLeft = findRightMost(current->left);\n rightMostOfLeft->right = current->right;\n current = current->left;\n }\n }\n\n // breaks when nodes are freed otherwise\n if(root)\n {\n root->left = nullptr;\n root->right = nullptr;\n }\n return res;\n }\n};",
"memory": "9400"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* findRightMost(TreeNode* node)\n {\n while(node->right)\n node = node->right;\n return node;\n }\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> res;\n TreeNode* current = root;\n while(current)\n {\n cout << current->val << endl;\n res.push_back(current->val);\n if(!current->left)\n {\n current = current->right;\n } else{\n TreeNode* rightMostOfLeft = findRightMost(current->left);\n rightMostOfLeft->right = current->right;\n current = current->left;\n }\n }\n\n if(root)\n {\n root->left = nullptr;\n root->right = nullptr;\n }\n return res;\n }\n};",
"memory": "9500"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> ans;\n TreeNode* temp=NULL;\n while(root!=NULL)\n {\n if(root->left!=NULL){\n ans.push_back(root->val);\n temp=root;\n root=root->left;\n TreeNode *temp2=root;\n while(root->right)\n {\n root=root->right;\n }\n root->right=temp->right;\n temp->left=NULL;\n temp->right=temp2;\n temp=temp->right;\n root=temp;\n }\n else\n { ans.push_back(root->val);\n root=root->right;\n }\n }\n return ans;\n }\n};",
"memory": "9600"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> result;\n TreeNode* curr= root;\n while(curr){\n result.push_back(curr->val);\n if(curr->left){\n TreeNode* left = curr->left;\n while(left->right){\n left = left->right;\n }\n left->right = curr->right;\n TreeNode* temp= curr;\n curr= curr->left;\n temp->left=NULL;\n }\n else{\n curr= curr->right;\n }\n\n }\n return result;\n }\n};",
"memory": "9700"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // Morris Inorder\n // T.C. = O(N) , S.C. = O(1)\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> ans;\n TreeNode* cur = root;\n while(cur!=NULL){\n if(cur->left == NULL){\n ans.push_back(cur->val);\n cur = cur->right;\n }\n else{\n TreeNode* prev = cur->left;\n while(prev->right!=NULL && prev->right!=cur){\n prev = prev->right;\n }\n\n if(prev->right==NULL){\n prev->right = cur;\n ans.push_back(cur->val);\n cur = cur->left;\n }\n else{\n prev->right = NULL;\n cur = cur->right;\n }\n }\n }\n return ans;\n }\n};",
"memory": "9700"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void preorder(TreeNode* root, vector<int>&ans){\n if(root == NULL){\n return;\n }\n ans.push_back(root->val);\n preorder(root->left, ans);\n preorder(root->right, ans);\n }\n\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int>ans;\n preorder(root, ans);\n return ans;\n }\n};",
"memory": "9800"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n \n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> ans;\n TreeNode* curr=root;\n while(curr){\n if(!curr->left){\n ans.push_back(curr->val);\n curr=curr->right;\n }\n else{\n TreeNode* prev=curr->left;\n while(prev->right && prev->right!=curr){\n prev=prev->right;\n }\n if(!prev->right){\n prev->right=curr;\n ans.push_back(curr->val);\n curr=curr->left;\n }\n else{\n curr=curr->right;\n prev->right=NULL;\n }\n }\n }\n return ans;\n }\n};",
"memory": "9900"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n if(!root) return {};\n vector<int> ans;\n stack<TreeNode*> st;\n st.push(root);\n\n while(!st.empty()) {\n auto top = st.top(); st.pop();\n ans.push_back(top->val);\n if(top->right)\n st.push(top->right);\n if(top->left)\n st.push(top->left);\n }\n return ans;\n }\n};",
"memory": "9900"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void pre(vector<int>&v,TreeNode* root){\n if(root == NULL) return;\n\n v.push_back(root->val);\n pre(v,root->left);\n pre(v,root->right);\n }\n\n\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int>result;\n // Through recursion\n // pre(result,root);\n // return result;\n\n // iterative through stack\n\n stack<TreeNode*>st;\n if(root == NULL) return result;\n st.push(root);\n\n while(!st.empty()){\n result.push_back(st.top()->val);\n TreeNode* temp=st.top();\n st.pop();\n if(temp->right != NULL)\n st.push(temp->right);\n if(temp->left != nullptr)\n st.push(temp->left);\n \n }\n return result;\n }\n\n\n};",
"memory": "10000"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void preorder(vector<int> &ans, TreeNode* root){\n if(root==nullptr){\n return;\n }\n ans.push_back(root->val);\n preorder(ans,root->left);\n preorder(ans,root->right);\n }\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> ans;\n preorder(ans,root);\n return ans;\n }\n};",
"memory": "10000"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void pt(TreeNode* root, vector<int>& result) {\n if (root == NULL) return;\n result.push_back(root->val);\n pt(root->left, result);\n pt(root->right, result);\n }\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> result;\n pt(root, result);\n return result;\n }\n};",
"memory": "10100"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 0 | {
"code": "/**\n * Definition for a binary tree Node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void fun(TreeNode* root,vector<int> &vec){\n if(!root)\n return;\n vec.push_back(root->val);\n fun(root->left,vec);\n fun(root->right,vec);\n }\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> reslt;\n fun(root,reslt);\n return reslt;\n }\n};",
"memory": "10100"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\n void preOrder(TreeNode* root, vector<int>& v) {\n if (!root)\n return;\n stack<TreeNode*> stk;\n stk.push(root);\n while (!stk.empty()) {\n TreeNode* curr = stk.top();\n stk.pop();\n v.push_back(curr->val);\n if (curr->right)\n stk.push(curr->right);\n if (curr->left)\n stk.push(curr->left);\n }\n }\n\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> traversal;\n preOrder(root, traversal);\n return traversal;\n }\n};",
"memory": "10200"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> v;\n if(root == NULL)\n return v;\n \n stack<TreeNode*> s;\n s.push(root);\n\n while(!s.empty()) {\n TreeNode* top = s.top();\n s.pop();\n v.push_back(top->val);\n\n if(top->right != NULL) s.push(top->right);\n if(top->left != NULL) s.push(top->left);\n }\n return v;\n }\n};",
"memory": "10200"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution\n{\n int const pre = 0;\n int const in = 1;\n int const post = 2;\n vector<vector<int>> res;\n\n void f(TreeNode * root)\n {\n if(!root) return;\n\n res[pre].push_back(root->val);\n f(root->left);\n res[in].push_back(root->val);\n f(root->right);\n res[post].push_back(root->val);\n }\n\npublic:\n vector<int> preorderTraversal(TreeNode* root)\n {\n\n res = vector<vector<int>> (3);\n\n f(root);\n \n return res[pre];\n }\n};",
"memory": "10900"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n std::vector<int> result;\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n if (root == nullptr) {\n return result;\n }\n result.emplace_back(root->val);\n if (root->left != nullptr) preorderTraversal(root->left);\n if (root->right != nullptr) preorderTraversal(root->right);\n\n return result; \n }\n};",
"memory": "11000"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> ans;\n vector<int> preorderTraversal(TreeNode* root) {\n if(!root) return ans;\n TreeNode* curr=root;\n ans.push_back(curr->val);\n if(curr->left) preorderTraversal(curr->left);\n if(curr->right) preorderTraversal(curr->right);\n return ans;\n }\n};",
"memory": "11100"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n\n if (root == NULL) {\n return vector<int>{};\n }\n\n vector<int> leftTraversal = preorderTraversal(root->left);\n leftTraversal.insert(leftTraversal.begin(), root->val);\n vector<int> rightTraversal = preorderTraversal(root->right);\n\n leftTraversal.insert(leftTraversal.end(), rightTraversal.begin(),\n rightTraversal.end());\n\n return leftTraversal;\n }\n};",
"memory": "11200"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate: vector<int> preorder(TreeNode* root, vector<int>& tree){\n if(root == nullptr) \n return {};\n\n tree.push_back(root->val);\n\n if(root->left != nullptr)\n preorder(root->left, tree);\n if(root->right != nullptr)\n preorder(root->right, tree);\n\n return tree;\n}\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int> tree;\n return preorder(root, tree); \n }\n};",
"memory": "11300"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> res;\n vector<int> preorderTraversal(TreeNode* root) {\n if(!root) return res;\n res.push_back(root->val);\n if(root->left) preorderTraversal(root->left);\n if(root->right) preorderTraversal( root->right);\n\n return res;\n \n }\n};",
"memory": "11400"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int>result;\n vector<int> preorderTraversal(TreeNode* root) {\n \n if(root==NULL){return {};}\n result.push_back(root->val);\n preorderTraversal(root->left);\n preorderTraversal(root->right);\n return result;\n }\n};",
"memory": "11500"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvector<int> ans;\n vector<int> preorderTraversal(TreeNode* root) {\n \n if(root==NULL)\n return {};\n cout<<root->val;\n ans.push_back(root->val);\n \n preorderTraversal(root->left);\n preorderTraversal(root->right);\n \n return ans;\n }\n};",
"memory": "11600"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\n vector<int> p;\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n if (root == NULL)\n return {};\n p.push_back(root->val);\n preorderTraversal(root->left);\n preorderTraversal(root->right);\n return p;\n }\n};",
"memory": "11600"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvector<int> preorderTraversal(TreeNode* root) {\n vector<int> result; // Initialize an empty vector to store traversal results\n\n // Base case: if the tree is empty, return an empty vector\n if (root == nullptr) {\n return result;\n }\n\n // Add the root node's value to the result vector\n result.push_back(root->val);\n\n // Recursively traverse the left subtree and append its result\n vector<int> leftTraversal = preorderTraversal(root->left);\n result.insert(result.end(), leftTraversal.begin(), leftTraversal.end());\n\n // Recursively traverse the right subtree and append its result\n vector<int> rightTraversal = preorderTraversal(root->right);\n result.insert(result.end(), rightTraversal.begin(), rightTraversal.end());\n\n return result;\n}\n\n};",
"memory": "11700"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n vector <int> trav;\n \n if (root == NULL) {\n return trav;\n }\n \n trav.push_back(root -> val);\n\n vector<int> leftTrav = preorderTraversal(root->left);\n\n trav.insert(trav.end(), leftTrav.begin(), leftTrav.end());\n vector<int> rightTrav = preorderTraversal(root->right);\n trav.insert(trav.end(), rightTrav.begin(), rightTrav.end());\n\n return trav;\n }\n};",
"memory": "11700"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n vector<int>res;\n if(root==NULL)return res;\n \n res.push_back(root->val);\n vector<int>left=preorderTraversal(root->left);\n vector<int>right=preorderTraversal(root->right);\n res.insert(res.end(),left.begin(),left.end());\n res.insert(res.end(),right.begin(),right.end());\n return res;\n }\n};",
"memory": "11800"
} |
144 | <p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> preorderTraversal(TreeNode* root) {\n if(root == NULL) return {};\n vector<int> left_tree = preorderTraversal(root->left);\n vector<int> right_tree = preorderTraversal(root->right);\n vector<int> result;\n result.push_back(root->val);\n result.insert(result.end(), left_tree.begin(), left_tree.end());\n result.insert(result.end(), right_tree.begin(), right_tree.end());\n return result;\n }\n};",
"memory": "11800"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int> temp;\n if(root==NULL) return temp;\n helpPostOrder(root , temp);\n return temp;\n }\n\n void helpPostOrder(TreeNode* root , vector<int>& temp){\n if(root->left) helpPostOrder(root->left , temp);\n if(root->right) helpPostOrder(root->right , temp);\n temp.push_back(root->val);\n }\n};",
"memory": "10000"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int> v;\n while(root){\n if(root->right==NULL){\n v.push_back(root->val);\n root=root->left;\n }\n else{\n TreeNode* pre=root->right;\n while(pre->left && pre->left!=root){\n pre=pre->left;\n }\n if(!pre->left){\n pre->left=root;\n v.push_back(root->val);\n root=root->right;\n }\n else{\n pre->left=NULL;\n root=root->left;\n }\n }\n }\n reverse(v.begin(),v.end());\n return v;\n }\n};",
"memory": "10000"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 0 | {
"code": "class Solution {\npublic:\n void traverse(TreeNode* root, vector<int>& res){\n if (root){\n traverse(root -> left, res);\n traverse(root -> right, res);\n res.push_back(root -> val);\n }\n }\n\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int> res;\n traverse(root, res);\n return res;\n }\n};",
"memory": "10100"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 0 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void Helper(TreeNode*node, vector<int>& result){\n if(node == nullptr) return ;\n Helper(node->left, result);\n Helper(node->right, result);\n result.push_back(node->val);\n }\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int> result;\n Helper(root, result);\n return result;\n }\n \n};",
"memory": "10100"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 1 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid solve(TreeNode* root,vector<int>& ans){\n if(root == NULL) return;\n\n //left->right->root\n \n solve(root->left,ans);\n \n solve(root->right,ans);\n ans.push_back(root->val);\n }\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int> ans;\n solve(root,ans);\n return ans;\n \n }\n};",
"memory": "10200"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 1 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void postorder(TreeNode *ptr, vector<int> &arr){\n if (ptr == nullptr){\n return;\n }\n postorder(ptr->left,arr);\n postorder(ptr->right,arr);\n arr.push_back(ptr->val);\n }\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int> arr;\n postorder(root,arr);\n return arr;\n }\n};",
"memory": "10200"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> arr; \n vector<int> postorderTraversal(TreeNode* root) {\n postorder(root);\n return arr;\n }\n \n void postorder(TreeNode* root) {\n if(root == nullptr){\n return;\n }\n postorder(root->left);\n // arr.push_back(root -> val);\n postorder(root->right);\n arr.push_back(root -> val);\n }\n};",
"memory": "10300"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void fun(vector<int>& res, TreeNode* root){\n if(root==nullptr)return;\n fun(res, root->left);\n fun(res, root->right);\n res.push_back(root->val);\n }\n\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int>res;\n fun(res, root);\n return res;\n }\n};",
"memory": "10300"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void postorder(TreeNode* root, vector<int> &ans)\n {\n if(!root)\n return;\n postorder(root->left, ans);\n postorder(root->right, ans);\n ans.push_back(root->val);\n \n }\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int> ans;\n postorder(root,ans);\n return ans;\n }\n};",
"memory": "10400"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> ans;\n\n void it(TreeNode *t)\n {\n if(t == NULL)\n return;\n it(t->left);\n it(t->right);\n ans.push_back(t->val);\n }\n\n vector<int> postorderTraversal(TreeNode* root) {\n it(root);\n return ans;\n }\n};",
"memory": "10400"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> foo;\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int> ans;\n if (!root) {\n return ans;\n }\n traverse(root, &ans);\n return foo;\n }\n\nprivate:\n void traverse(TreeNode* root, vector<int>* ans) {\n if (!root) {\n return;\n }\n traverse(root->left, ans);\n traverse(root->right, ans);\n ans->push_back(root->val);\n foo.push_back(root->val);\n }\n};",
"memory": "10500"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n// preorder postorder and inorder together using 1 stack only\n\n\ntuple<vector<int>,vector<int>,vector<int>> Allorders(TreeNode* root){\n vector<int> preorder,inorder,postorder;\n stack<pair<TreeNode*,int>>st;\n st.push({root,1});\n while(!st.empty()){\n TreeNode* top=st.top().first;\n int num=st.top().second;\n st.pop();\n if(num==1){\n preorder.push_back(top->val);\n st.push({top,2});\n if(top->left)st.push({top->left,1});\n }\n else if(num==2){\n inorder.push_back(top->val);\n st.push({top,3});\n if(top->right)st.push({top->right,1});\n }\n else{\n postorder.push_back(top->val);\n }\n }\n\ntuple<vector<int>,vector<int>,vector<int>>tup={preorder,inorder,postorder};\nreturn tup;\n \n}\n vector<int> postorderTraversal(TreeNode* root) {\n if(root==nullptr) return {};\n tuple<vector<int>,vector<int>,vector<int>>tup=Allorders(root);\n vector<int>postorder=get<2>(tup);\n return postorder;\n }\n};",
"memory": "10600"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution\n{\n int const pre = 0;\n int const in = 1;\n int const post = 2;\n vector<vector<int>> res;\n\n void f(TreeNode * root)\n {\n if(!root) return;\n\n res[pre].push_back(root->val);\n f(root->left);\n res[in].push_back(root->val);\n f(root->right);\n res[post].push_back(root->val);\n }\n\npublic:\n vector<int> postorderTraversal(TreeNode* root)\n {\n\n res = vector<vector<int>> (3);\n\n f(root);\n \n return res[post];\n }\n};",
"memory": "10700"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n void getTreeTraversalHelper(TreeNode* root, vector<vector<int>>& traversals) {\n if (!root) {\n return;\n }\n traversals[0].push_back(root->val);\n getTreeTraversalHelper(root->left, traversals);\n traversals[1].push_back(root->val);\n getTreeTraversalHelper(root->right, traversals);\n traversals[2].push_back(root->val);\n }\npublic:\n vector<int> postorderTraversal(TreeNode* root) {\n vector<vector<int>> traversals(3); // 0=pre 1=in 2=post\n getTreeTraversalHelper(root, traversals);\n return traversals[2];\n }\n};",
"memory": "10800"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> postorderTraversal(TreeNode* root) {\n if (root == nullptr)\n return {};\n vector<int> left = postorderTraversal(root->left);\n vector<int> right = postorderTraversal(root->right);\n for (auto it : right)\n left.push_back(it);\n left.push_back(root->val);\n return left;\n }\n};",
"memory": "10900"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> postorderTraversal(TreeNode* root) {\n if(root == NULL)\n {\n vector<int> a;\n return a;\n }\n\n vector<int> l = postorderTraversal(root->left);\n vector<int> r = postorderTraversal(root->right);\n\n for(auto it: r)\n {\n l.push_back(it);\n }\n l.push_back(root->val);\n return l;\n }\n};",
"memory": "10900"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> IT;\n vector<int> postorderTraversal(TreeNode* root) {\n if(root != nullptr){\n if(root->left!=nullptr) postorderTraversal(root->left);\n if(root->right) postorderTraversal(root->right);\n IT.push_back(root->val);\n }\n return IT;\n }\n};",
"memory": "11000"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int>v;\n if(root==NULL){\n return v;\n }\n\nvector<int>x;\n if(root->left!=NULL){\n v=postorderTraversal(root->left);\n\n }\n if(root->right!=NULL){\n x=postorderTraversal(root->right);\n for(auto k:x){\n v.push_back(k);\n }\n }\n v.push_back(root->val);\n return v;\n\n \n }\n};",
"memory": "11100"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> postorderTraversal(TreeNode* root) { // stack解 12:11 to 13:18 / 遞迴解 13:20 to 13:25\n // 用遞迴\n if (!root) return {};\n vector<int> ans, temp;\n ans = postorderTraversal(root->left);\n temp = postorderTraversal(root->right);\n ans.insert(ans.end(),temp.begin(),temp.end());\n ans.push_back(root->val);\n return ans;\n }\n};",
"memory": "11200"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> result;\n vector<int> postorderTraversal(TreeNode* root) {\n if(root==NULL){return{};}\n postorderTraversal(root->left);\n postorderTraversal(root->right);\n result.push_back(root->val);\n return result;\n }\n};",
"memory": "11300"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> postorderTraversal(TreeNode* root) {\n vector<int> v1;\n if(root == nullptr) {\n return v1;\n }\n vector<int> v2;\n v1 = postorderTraversal(root->left);\n v2 = postorderTraversal(root->right);\n for(int i = 0;i < v2.size();i++) {\n v1.push_back(v2[i]);\n }\n v1.push_back(root->val);\n return v1;\n }\n};",
"memory": "11300"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n vector<int> res;\npublic:\n vector<int> postorderTraversal(TreeNode* root) {\n if(!root) return res;\n if(root->left) postorderTraversal(root->left);\n if(root->right) postorderTraversal(root->right);\n res.push_back(root->val);\n\n return res;\n }\n};",
"memory": "11400"
} |
145 | <p>Given the <code>root</code> of a binary tree, return <em>the postorder traversal of its nodes' values</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
<p><strong>Explanation:</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the tree is in the range <code>[0, 100]</code>.</li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively? | 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n vector<int> p;\npublic:\n vector<int> postorderTraversal(TreeNode* root) {\n if (root == NULL)\n return {};\n postorderTraversal(root->left);\n postorderTraversal(root->right);\n p.push_back(root->val);\n return p;\n }\n};",
"memory": "11400"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) {\n vector<int> v;\n ListNode* curr = head;\n while (curr) {\n v.push_back(curr->val);\n curr = curr->next;\n }\n sort(v.begin(), v.end());\n curr = head;\n for (auto& n : v) {\n curr->val = n;\n curr = curr->next;\n }\n return head;\n }\n};\n\nauto _ = [](){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n std::ofstream out(\"user.out\", std::ios::out | std::ios::binary);\n out.rdbuf()->pubsetbuf(nullptr, 256);\n std::noskipws(std::cin);\n \n for (string s; getline(cin, s); out << \"\\n\") {\n vector<int> v;\n string temp;\n for (int i=1; i<s.size(); i++) {\n if (s[i] == ',' || s[i] == ']') {\n v.push_back(stoi(temp));\n temp = \"\";\n } else {\n temp += s[i];\n }\n }\n sort(v.begin(), v.end());\n out << '[';\n for (int i=0; i<v.size(); i++) {\n out << v[i];\n if (i < v.size()-1) out << ',';\n }\n out << ']';\n }\n\n std::skipws(std::cin);\n out.flush();\n exit(0);\n return 0;\n}();",
"memory": "9300"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) {\n vector<int> v;\n ListNode* curr = head;\n while (curr) {\n v.push_back(curr->val);\n curr = curr->next;\n }\n sort(v.begin(), v.end());\n curr = head;\n for (auto& n : v) {\n curr->val = n;\n curr = curr->next;\n }\n return head;\n }\n};\n\nauto _ = [](){\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n std::ofstream out(\"user.out\", std::ios::out | std::ios::binary);\n out.rdbuf()->pubsetbuf(nullptr, 256);\n std::noskipws(std::cin);\n \n for (string s; getline(cin, s); out << \"\\n\") {\n vector<int> v;\n string temp;\n for (int i=1; i<s.size(); i++) {\n if (s[i] == ',' || s[i] == ']') {\n v.push_back(stoi(temp));\n temp = \"\";\n } else {\n temp += s[i];\n }\n }\n sort(v.begin(), v.end());\n out << '[';\n for (int i=0; i<v.size(); i++) {\n out << v[i];\n if (i < v.size()-1) out << ',';\n }\n out << ']';\n }\n\n std::skipws(std::cin);\n out.flush();\n exit(0);\n return 0;\n}();",
"memory": "9300"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) {\n ListNode* dummyNode = new ListNode();\n ListNode* curr = head;\n while(curr){\n ListNode* prev = dummyNode;\n while(prev->next && prev->next->val <= curr->val){\n prev = prev->next;\n }\n ListNode* next = curr->next;\n curr->next = prev->next;\n prev->next = curr;\n curr = next;\n }\n\n return dummyNode->next;\n }\n};",
"memory": "14200"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* reverse(ListNode*head) {\n ListNode* curr = head;\n ListNode* prev = NULL;\n while (curr) {\n ListNode* nextNode = curr->next;\n curr->next = prev;\n prev = curr;\n curr = nextNode;\n }\n return prev;\n }\n\n ListNode* insertItem(ListNode*head, ListNode*item) {\n item->next = NULL;\n if (!head) {\n return item;\n }\n\n if (item->val >= head->val) {\n item->next = head;\n return item;\n }\n ListNode* curr = head;\n ListNode* prev = NULL;\n while (curr && curr->val>item->val) {\n prev = curr;\n curr = curr->next;\n }\n \n prev->next = item;\n item->next = curr;\n return head;\n\n }\n ListNode* insertionSortList(ListNode* head) {\n ListNode* curr = head;\n ListNode* prev = NULL;\n\n while (curr) {\n ListNode* nextNode = curr->next;\n prev = insertItem(prev, curr);\n curr = nextNode;\n }\n\n return reverse(prev);\n }\n};",
"memory": "14300"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 0 | {
"code": "class Solution {\n\npublic:\n\n ListNode* sortedInsert(ListNode* head, ListNode* node) {\n\n if (head == nullptr || head->val >= node->val) {\n\n node->next = head;\n\n return node;\n\n }\n\n ListNode* current = head;\n\n while (current->next != nullptr && current->next->val < node->val) {\n\n current = current->next;\n\n }\n\n node->next = current->next;\n\n current->next = node;\n\n return head;\n\n }\n\n ListNode* insertionSortList(ListNode* head) {\n\n ListNode* sorted = nullptr; \n\n ListNode* current = head;\n\n while (current != nullptr) {\n\n ListNode* next = current->next; \n\n sorted = sortedInsert(sorted, current);\n\n current = next; \n\n }\n\n return sorted;\n\n }\n\n};",
"memory": "14400"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) {\n if (head == NULL or head->next == NULL) {\n return head;\n }\n ListNode* temp = head;\n int idx = 0;\n while (temp->next != NULL) {\n temp = temp->next;\n idx++;\n }\n ListNode* prev = head;\n for (int i = 0; i < idx; i++) {\n temp = head;\n for (int j = 0; j < idx - i; j++) {\n if (temp->val > temp->next->val) {\n int tempdata = temp->next->val;\n temp->next->val = temp->val;\n temp->val = tempdata;\n }\n temp = temp->next;\n }\n }\n return head;\n }\n};",
"memory": "14400"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast;\n\n while(slow)\n {\n fast = slow;\n while(fast)\n {\n if(fast->val < slow->val)\n {\n int temp = slow->val;\n slow->val = fast->val;\n fast->val = temp;\n }\n fast = fast->next;\n }\n slow = slow->next;\n }\n return head; \n }\n};",
"memory": "14500"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast;\n\n while(slow)\n {\n fast = slow;\n while(fast)\n {\n if(fast->val < slow->val)\n {\n int temp = slow->val;\n slow->val = fast->val;\n fast->val = temp;\n }\n fast = fast->next;\n }\n slow = slow->next;\n }\n return head; \n }\n};",
"memory": "14500"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) {\n if (head == NULL || head->next == NULL)\n return head;\n ListNode* dummy = new ListNode(INT_MIN);\n ListNode* uList = head;\n while (uList != NULL) {\n ListNode* temp = uList;\n uList = uList->next;\n temp->next = NULL;\n ListNode* sList = dummy;\n while (sList->next != NULL && sList->next->val < temp->val) {\n sList = sList->next;\n }\n ListNode* rand = sList->next;\n sList->next = temp;\n temp->next = rand;\n }\n return dummy->next;\n }\n};",
"memory": "14600"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) {\n \n ListNode* curr=head;\n vector<int> v;\n\n while(curr){\n v.push_back(curr->val);\n curr=curr->next;\n }\n\n sort(v.begin(),v.end());\n\n int idx=0;\n curr=head;\n\n while(curr){\n curr->val=v[idx];\n idx++;\n curr=curr->next;\n }\n\n return head;\n \n }\n};",
"memory": "14600"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 2 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) \n {\n if(!head)\n return head;\n\n vector<int>v;\n ListNode* temp=head;\n\n while(temp)\n {\n v.push_back(temp->val);\n temp=temp->next;\n }\n\n // Insertion sort\n for(int i=0;i<v.size();i++)\n {\n int element=v[i];\n int j=i-1;\n while(j>=0&&v[j]>element)\n {\n swap(v[j],v[j+1]);\n j--;\n }\n v[j+1]=element;\n }\n\n // v--> 1,2,3,4\n\n ListNode* curr=head;\n int k=0;\n\n while(curr)\n {\n curr->val=v[k];\n curr=curr->next;\n k++;\n }\n return head;\n \n }\n};",
"memory": "14700"
} |
147 | <p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list's head</em>.</p>
<p>The steps of the <strong>insertion sort</strong> algorithm:</p>
<ol>
<li>Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.</li>
<li>At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.</li>
<li>It repeats until no input elements remain.</li>
</ol>
<p>The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.</p>
<img alt="" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" style="height:180px; width:300px" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg" style="width: 422px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg" style="width: 542px; height: 222px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li>
<li><code>-5000 <= Node.val <= 5000</code></li>
</ul>
| 3 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* insertionSortList(ListNode* head) {\n if(head == NULL || head->next == NULL) return head;\n //new list\n ListNode* newHead = new ListNode(head->val);\n ListNode* prev = NULL, *curr = newHead;\n // old list\n ListNode* temp = head->next;\n\n while(temp) {\n curr = newHead;\n prev = NULL;\n while(curr && curr->val < temp->val) {\n prev = curr;\n curr = curr->next;\n }\n if(curr == NULL) {\n prev->next = new ListNode(temp->val);\n } else {\n if(prev == NULL) {\n ListNode* thead = new ListNode(temp->val);\n thead -> next = newHead;\n newHead = thead;\n } else {\n prev-> next = new ListNode(temp->val);\n prev->next->next = curr;\n }\n }\n temp = temp -> next;\n }\n return newHead;\n }\n};",
"memory": "14800"
} |
148 | <p>Given the <code>head</code> of a linked list, return <em>the list after sorting it in <strong>ascending order</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 450px; height: 194px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" style="width: 550px; height: 184px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> head = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you sort the linked list in <code>O(n logn)</code> time and <code>O(1)</code> memory (i.e. constant space)?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\n ListNode* merge(ListNode* head, ListNode* mid){\n ListNode* newHead = NULL, *temp = NULL, *curr = NULL;\n while(head || mid){\n if(!head){\n temp = mid;\n mid = mid->next;\n temp->next = NULL;\n if(!newHead) newHead = temp, curr = temp;\n else curr->next = temp, curr = curr->next;\n }\n else if(!mid){\n temp = head;\n head = head->next;\n temp->next = NULL;\n if(!newHead) newHead = temp, curr = temp;\n else curr->next = temp, curr = curr->next;\n }\n else{\n if(head->val <= mid->val){\n temp = head;\n head = head->next;\n temp->next = NULL;\n if(!newHead) newHead = temp, curr = temp;\n else curr->next = temp, curr = curr->next;\n }\n else{\n temp = mid;\n mid = mid->next;\n temp->next = NULL;\n if(!newHead) newHead = temp, curr = temp;\n else curr->next = temp, curr = curr->next;\n }\n }\n }\n return newHead;\n }\n ListNode* mergeSort(ListNode* head){\n if(!head || !head->next) return head;\n ListNode *slow = head, *fast = head, *prev = NULL;\n while(fast && fast->next){\n prev = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n ListNode* mid = slow;\n prev->next = NULL;\n head = mergeSort(head);\n mid = mergeSort(mid);\n head = merge(head, mid);\n return head;\n }\npublic:\n ListNode* sortList(ListNode* head) {\n return mergeSort(head);\n }\n};",
"memory": "11656"
} |
148 | <p>Given the <code>head</code> of a linked list, return <em>the list after sorting it in <strong>ascending order</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 450px; height: 194px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" style="width: 550px; height: 184px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> head = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you sort the linked list in <code>O(n logn)</code> time and <code>O(1)</code> memory (i.e. constant space)?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n if (!head || !head->next) {\n return head;\n }\n \n ListNode* mid = getMid(head);\n ListNode* left = sortList(head);\n ListNode* right = sortList(mid);\n \n return merge(left, right);\n }\n \nprivate:\n ListNode* getMid(ListNode* head) {\n ListNode* prev = nullptr;\n ListNode* slow = head;\n ListNode* fast = head;\n \n while (fast && fast->next) {\n prev = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n \n if (prev) {\n prev->next = nullptr;\n }\n \n return slow;\n }\n \n ListNode* merge(ListNode* left, ListNode* right) {\n ListNode dummy(0);\n ListNode* tail = &dummy;\n \n while (left && right) {\n if (left->val < right->val) {\n tail->next = left;\n left = left->next;\n } else {\n tail->next = right;\n right = right->next;\n }\n tail = tail->next;\n }\n \n tail->next = left ? left : right;\n \n return dummy.next;\n }\n};",
"memory": "11656"
} |
148 | <p>Given the <code>head</code> of a linked list, return <em>the list after sorting it in <strong>ascending order</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 450px; height: 194px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" style="width: 550px; height: 184px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> head = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you sort the linked list in <code>O(n logn)</code> time and <code>O(1)</code> memory (i.e. constant space)?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n // Function to find the middle of the linked list\n ListNode* findMiddle(ListNode* head) {\n if (!head) return nullptr;\n ListNode* slow = head;\n ListNode* fast = head->next;\n while (fast && fast->next) {\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n }\n \n // Function to merge two sorted linked lists\n ListNode* mergeLists(ListNode* l1, ListNode* l2) {\n ListNode dummy(0);\n ListNode* tail = &dummy;\n while (l1 && l2) {\n if (l1->val < l2->val) {\n tail->next = l1;\n l1 = l1->next;\n } else {\n tail->next = l2;\n l2 = l2->next;\n }\n tail = tail->next;\n }\n // Connect the remaining part of the list\n tail->next = l1 ? l1 : l2;\n return dummy.next;\n }\n\n // Main function to sort the list\n ListNode* sortList(ListNode* head) {\n if (!head || !head->next) return head; // Base case for recursion\n \n // Split the list into two halves\n ListNode* mid = findMiddle(head);\n ListNode* secondHalf = mid->next;\n mid->next = nullptr; // Break the link between the two halves\n \n // Recursively sort each half\n ListNode* left = sortList(head);\n ListNode* right = sortList(secondHalf);\n \n // Merge the sorted halves\n return mergeLists(left, right);\n }\n};",
"memory": "12598"
} |
148 | <p>Given the <code>head</code> of a linked list, return <em>the list after sorting it in <strong>ascending order</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 450px; height: 194px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" style="width: 550px; height: 184px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> head = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you sort the linked list in <code>O(n logn)</code> time and <code>O(1)</code> memory (i.e. constant space)?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* middleNode(ListNode* head) {\n ListNode* fast = head;\n ListNode* slow = head;\n while(fast->next!=NULL){\n fast = fast->next;\n if(fast->next!=NULL){\n fast = fast->next;\n slow = slow->next;\n }\n }\n return slow;\n\n }\n ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {\n ListNode * newHead ;\n ListNode *temp;\n\n if(list1 == NULL && list2 == NULL)\n return NULL;\n else if(list1 == NULL && list2!= NULL)\n return list2;\n else if(list2 == NULL && list1!=NULL)\n return list1;\n\n if(list1->val<list2->val){\n newHead = list1;\n list1 = list1->next;\n }\n else{\n newHead = list2;\n list2 = list2->next;\n }\n temp = newHead;\n while(list1!= NULL && list2!= NULL){\n if(list1->val < list2->val){\n temp->next = list1;\n temp = list1;\n list1 = list1->next;\n if(list1 == NULL){\n temp ->next = list2;\n }\n }\n else{\n temp->next = list2;\n temp = list2;\n list2 = list2->next;\n if(list2 == NULL){\n temp ->next = list1;\n }\n }\n }\n if(list1 == NULL && list2 != NULL)\n temp->next = list2;\n\n else if(list2 == NULL && list1 != NULL) \n temp->next = list1;\n\n return newHead;\n }\n ListNode* sortList(ListNode* head) {\n if(head == NULL || head->next == NULL)\n return head;\n\n ListNode * middle = middleNode(head);\n\n ListNode* right = middle->next;\n middle->next = NULL;\n ListNode* left = head;\n\n left = sortList(left);\n right = sortList(right);\n\n return mergeTwoLists(left,right);\n }\n};",
"memory": "12598"
} |
148 | <p>Given the <code>head</code> of a linked list, return <em>the list after sorting it in <strong>ascending order</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 450px; height: 194px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" style="width: 550px; height: 184px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> head = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you sort the linked list in <code>O(n logn)</code> time and <code>O(1)</code> memory (i.e. constant space)?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* middleNode(ListNode* head) {\n ListNode* fast=head;\n ListNode* slow=head;\n ListNode* temp=head;\n int s=0;\n while(temp!=NULL){\n temp=temp->next;\n s++;\n }\n while(fast->next!=NULL && fast->next->next!=NULL){\n fast=fast->next->next;\n slow=slow->next;\n }\n // if(s%2==0){\n // return slow->next;\n // }\n // else{\n return slow;\n // }\n\n \n }\n ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {\n ListNode dummy;\n ListNode* tail = &dummy;\n while (l1 != nullptr && l2 != nullptr) {\n if (l1->val <= l2->val) {\n tail->next = l1;\n l1 = l1->next; \n } else {\n tail->next = l2; \n l2 = l2->next; \n }\n tail = tail->next; \n }\n if (l1 != nullptr) {\n tail->next = l1;\n } else {\n tail->next = l2;\n }\n return dummy.next;\n }\n ListNode* sortList(ListNode* head) {\n if(head==NULL ||head->next==NULL){return head;}\n ListNode* middle=middleNode(head);\n ListNode*right=middle->next;\n middle->next=NULL;\n ListNode*left=head;\n left=sortList(left);\n right=sortList(right);\n return mergeTwoLists(left,right);\n }\n};",
"memory": "13541"
} |
148 | <p>Given the <code>head</code> of a linked list, return <em>the list after sorting it in <strong>ascending order</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 450px; height: 194px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" style="width: 550px; height: 184px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> head = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you sort the linked list in <code>O(n logn)</code> time and <code>O(1)</code> memory (i.e. constant space)?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n return mergeSort(head);\n }\n ListNode* mergeSort(ListNode*& head){\n if (head == nullptr || head->next == nullptr) return head; // Caso base, final de la lista\n\n ListNode* middle = splitList(head).second; //Divido la lista por el nodo de la mitad que sera middle\n ListNode* left = head; //Nodo a la izquierda de middle, que ordenara la parte anterior a este\n ListNode* right = middle; //Nodo que ordenara la parte depúes de middle\n\n left = mergeSort(left); //Vuelvo a dividir las listas recurisvamente\n right = mergeSort(right); \n\n return merge(left, right); \n }\n ListNode* merge(ListNode* left,ListNode* right){\n ListNode dummy(0);\n ListNode* tail = &dummy; //Creo una lista donde insertare los nodos ordenados\n while(left != nullptr && right != nullptr){ //Realizo la ordenacion hasta que se acaba una lista\n if(left->val < right->val){\n tail->next = left;\n left = left->next;\n }else{\n tail->next = right;\n right = right->next;\n }\n tail = tail->next;\n }\n\n tail->next = (left != nullptr) ? left : right;\n\n\n return dummy.next;\n }\n\n std::pair<ListNode*, ListNode*> splitList(ListNode* head) {\n int length = countNodes(head); // Contar los nodos de la lista\n if (length == 0) return {nullptr, nullptr};\n \n int mid = length / 2;\n ListNode* current = head;\n ListNode* prev = nullptr;\n for (int i = 0; i < mid; i++) {\n prev = current;\n current = current->next;\n }\n \n if (prev != nullptr) {\n prev->next = nullptr; // Dividir la lista\n }\n \n return {head, current}; // Retorna las dos mitades\n }\n int countNodes(ListNode* head) {\n int count = 0;\n ListNode* current = head;\n while (current != nullptr) {\n count++;\n current = current->next;\n }\n return count;\n }\n};",
"memory": "13541"
} |
148 | <p>Given the <code>head</code> of a linked list, return <em>the list after sorting it in <strong>ascending order</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 450px; height: 194px;" />
<pre>
<strong>Input:</strong> head = [4,2,1,3]
<strong>Output:</strong> [1,2,3,4]
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg" style="width: 550px; height: 184px;" />
<pre>
<strong>Input:</strong> head = [-1,5,3,4,0]
<strong>Output:</strong> [-1,0,3,4,5]
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> head = []
<strong>Output:</strong> []
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.</li>
<li><code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you sort the linked list in <code>O(n logn)</code> time and <code>O(1)</code> memory (i.e. constant space)?</p>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n // Function to find the middle of the linked list\n ListNode* findMiddle(ListNode* head) {\n if (!head) return nullptr;\n ListNode* slow = head;\n ListNode* fast = head->next;\n while (fast && fast->next) {\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n }\n \n // Function to merge two sorted linked lists\n ListNode* mergeLists(ListNode* l1, ListNode* l2) {\n ListNode dummy(0);\n ListNode* tail = &dummy;\n while (l1 && l2) {\n if (l1->val < l2->val) {\n tail->next = l1;\n l1 = l1->next;\n } else {\n tail->next = l2;\n l2 = l2->next;\n }\n tail = tail->next;\n }\n // Connect the remaining part of the list\n tail->next = l1 ? l1 : l2;\n return dummy.next;\n }\n\n // Main function to sort the list\n ListNode* sortList(ListNode* head) {\n if (!head || !head->next) return head; // Base case for recursion\n \n // Split the list into two halves\n ListNode* mid = findMiddle(head);\n ListNode* secondHalf = mid->next;\n mid->next = nullptr; // Break the link between the two halves\n \n // Recursively sort each half\n ListNode* left = sortList(head);\n ListNode* right = sortList(secondHalf);\n \n // Merge the sorted halves\n return mergeLists(left, right);\n }\n};",
"memory": "14483"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.