id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
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* head1) {\n if(head1==NULL ||head1->next==NULL){\n return head1;\n }\n ListNode * head=head1;\nhead1=head1->next;\nhead->next=NULL;\n ListNode * tail,*maintail=head;\n ListNode * temp;\n while(head1!=NULL){\n temp=head1;\nhead1=head1->next;\ntemp->next=NULL;\nif(temp->val<head->val){\n temp->next=head;\n head=temp;\n}\nelse if(temp->val>maintail->val){\nmaintail->next=temp;\nmaintail=maintail->next;\n}\nelse{\n tail=head;\n while(1){\n \n if(tail->next==NULL){\n tail->next=temp;\n break;\n }\n else if(tail->next->val<temp->val){\n tail=tail->next;\n }\n else{\n temp->next=tail->next;\n tail->next=temp;\n break;\n }\n}\n}\n }\n return head;\n }\n};",
"memory": "14483"
} |
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* findMid(ListNode* head) {\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 ListNode* merge(ListNode* l1, ListNode* l2) {\n ListNode dummy(-1);\n ListNode* tail = &dummy;\n \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 \n if (l1) {\n tail->next = l1;\n } else {\n tail->next = l2;\n }\n \n return dummy.next;\n }\n\n ListNode* sortList(ListNode* head) {\n if (!head || !head->next) return head;\n\n ListNode* mid = findMid(head);\n ListNode* left = head;\n ListNode* right = mid->next;\n mid->next = nullptr;\n\n left = sortList(left);\n right = sortList(right);\n\n return merge(left, right);\n }\n};",
"memory": "15426"
} |
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 constexpr int MinValue = -1e5;\n constexpr int MaxValue = 1e5;\n array<int, (MaxValue - MinValue) + 1> counter;\n // MinValue = -3, MaxValue = 4;\n // arr<int, 8>;\n auto* cur = head;\n while (cur != nullptr) {\n counter[cur->val + abs(MinValue)] += 1;\n cur = cur->next;\n }\n\n cur = head;\n for (int i = 0; i < counter.size() && cur != nullptr; ++i) {\n int targetValue = i + MinValue;\n while (counter[i] > 0 && cur != nullptr) {\n cur->val = targetValue;\n cur = cur->next;\n --counter[i];\n }\n }\n\n return head;\n }\n};",
"memory": "16368"
} |
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 */\n #define MAX_SIZE 50000\nclass Solution {\npublic:\n /*void printList(ListNode* head, int max){\n cout<<\"[\";\n int i=0;\n while(head &&i++<max)\n {\n cout<<head->val<<\",\";\n head=head->next;\n }\n cout<<\"]\"<<endl;\n }*/\n void printList(ListNode* head){\n cout<<\"[\";\n while(head)\n {\n cout<<head->val<<\",\";\n head=head->next;\n }\n cout<<\"]\"<<endl;\n }\n void qsortList(ListNode* head,int len)\n {\n //printList(head,maxLen);\n if(len==2)\n {\n if(head->next->val<head->val)\n {\n swap(head->next->val,head->val);\n }\n return;\n }\n ListNode* pivot=head;\n int i;\n for(i=0;i<len/2;i++)\n {\n pivot=pivot->next;\n }\n int pv=pivot->val;\n swap(pivot->val,head->val);\n //cout<<\"pv=\"<<pv<<\" pivot->val=\"<<pivot->val<<\" head->val=\"<<head->val<<endl;\n pivot=head;\n ListNode* cur=head;\n ListNode* prev=nullptr;\n i=0;\n for(int j=0;j<len;j++)\n {\n if(cur->val<=pv)\n {\n prev=pivot;\n i++;\n swap(pivot->val,cur->val);\n pivot=pivot->next;\n }\n cur=cur->next;\n }\n //cout<<\"i=\"<<i<<\" pivot->val=\"<<pivot->val<<endl;\n if(prev)\n {\n //cout<<\"prev-val=\"<<prev->val<<endl;\n swap(prev->val,head->val);\n }\n //printList(head);\n if(i>2)\n {\n qsortList(head,i-1);\n }\n if(len-i>1)\n {\n qsortList(pivot,len-i);\n }\n \n }\n ListNode* sortList(ListNode* head) {\n if(!head || !head->next)\n {\n return head;\n }\n int len=0;\n ListNode* cur=head;\n while(cur)\n {\n cur=cur->next;\n len++;\n }\n //cout<<\"len=\"<<len<<endl;\n qsortList(head,len);\n return head;\n }\n};",
"memory": "17311"
} |
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 */\n\nusing Node = ListNode;\nclass Solution {\npublic:\n Node* thead, *cur, *tmp;\n\n int lengthList(ListNode* node){\n if (node == nullptr) return 0;\n return 1 + lengthList(node->next); \n }\n\n Node* merge(Node* node1, Node* node2){\n thead = cur = nullptr;\n while (node1 != nullptr && node2 != nullptr){\n if (node1->val <= node2->val){\n tmp = node1;\n node1 = node1->next;\n } else {\n tmp = node2;\n node2 = node2->next;\n }\n if (thead == nullptr){\n thead = cur = tmp;\n } else {\n cur->next = tmp;\n cur = tmp;\n }\n cur->next = nullptr;\n }\n while (node1 != nullptr){\n tmp = node1;\n node1 = node1->next;\n if (thead == nullptr){\n thead = cur = tmp;\n } else {\n cur->next = tmp;\n cur = tmp;\n }\n cur->next = nullptr;\n }\n while (node2 != nullptr){\n tmp = node2;\n node2 = node2->next;\n if (thead == nullptr){\n thead = cur = tmp;\n } else {\n cur->next = tmp;\n cur = tmp;\n }\n cur->next = nullptr;\n }\n return thead;\n }\n\n Node* sort(Node* head, int n){\n if (n <= 1 || head == nullptr) return head;\n Node* cur = head, *tmp = nullptr;\n for (int i = 1; i <= n/2; i++){\n tmp = cur;\n cur = cur->next;\n }\n tmp->next = nullptr;\n return merge(sort(head, n/2), sort(cur, n - n/2));\n }\n\n ListNode* sortList(ListNode* head) {\n int n = lengthList(head);\n return sort(head, n);\n }\n};",
"memory": "18253"
} |
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 int getLength(ListNode* head) {\n int l = 0;\n while (head) {\n ++l;\n head = head->next;\n }\n return l;\n }\n\n pair<ListNode*, ListNode*> quicksort(ListNode* head, int len) {\n if (!head || !head->next) return {head, head};\n\n ListNode* pivot;\n int pnum = rand() % len;\n if (!pnum) {\n pivot = head;\n head = head->next;\n } else {\n auto cur = head;\n while(--pnum)\n cur = cur->next;\n pivot = cur->next;\n cur->next = pivot->next;\n }\n pivot->next = NULL;\n\n ListNode *ll = NULL, *rl = NULL;\n int llen = 0, rlen = 0;\n while (head) {\n ListNode* nxt = head->next;\n if (head->val < pivot->val) {\n head->next = ll;\n ll = head;\n ++llen;\n } else {\n head->next = rl;\n rl = head;\n ++rlen;\n }\n\n head = nxt;\n }\n\n auto [lh, lt] = quicksort(ll, llen);\n auto [rh, rt] = quicksort(rl, rlen);\n\n ListNode *mh = lh, *mt = rt;\n if (!lt)\n mh = pivot;\n else\n lt->next = pivot;\n\n pivot->next = rh;\n if (!rt)\n mt = pivot;\n\n return {mh, mt};\n }\n\npublic:\n ListNode* sortList(ListNode* head) {\n return quicksort(head, getLength(head)).first;\n }\n};\n\nauto _ = [](){ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); return 0;}();",
"memory": "19196"
} |
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 int maxVal = INT_MIN;\n int minVal = INT_MAX;\n\n // First pass - get max and min\n ListNode* currPtr = head;\n while (currPtr) {\n maxVal = max(maxVal, currPtr->val);\n minVal = min(minVal, currPtr->val);\n currPtr = currPtr->next;\n }\n\n // Second pass - create counting vector and enumerate\n vector<int> counts(maxVal - minVal + 1, 0);\n currPtr = head;\n while (currPtr) {\n counts[(currPtr->val) - minVal]++;\n currPtr = currPtr->next;\n }\n\n // Third pass - reset linked list using counting vector\n currPtr = head;\n for (int i = 0; i < counts.size(); ++i) {\n while (counts[i] > 0) {\n currPtr->val = i + minVal;\n currPtr = currPtr->next;\n counts[i]--;\n }\n }\n return head;\n }\n\n};",
"memory": "20138"
} |
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==NULL || head-> next==NULL) return head;\n\n ListNode *temp=head;\n int cnt=1;\n while(temp->next!=NULL) {\n cnt++;\n temp=temp->next;\n }\n\n cout << cnt;\n\n temp=head;\n vector <int> vec(cnt);\n for (int i=0;i<cnt;i++) {\n vec[i]=temp->val;\n temp=temp->next;\n }\n\n sort (vec.begin(),vec.end());\n\n temp=head;\n for (int i=0;i<cnt;i++) {\n temp->val=vec[i];\n temp=temp->next;\n }\n \n return head;\n }\n};",
"memory": "21081"
} |
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 int n = 0;\n ListNode* curr = head;\n while(curr){\n n++;\n curr = curr->next;\n }\n curr = head;\n vector<int> array(n);\n for(int i = 0; i < n; i++){\n array[i] = curr->val;\n curr = curr->next;\n }\n sort(array.begin(), array.end());\n curr = head;\n for(int i = 0; i < n; i++){\n curr->val = array[i];\n curr = curr->next;\n }\n return head;\n }\n};",
"memory": "22023"
} |
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\n if(head == NULL || head->next == NULL) {\n return head;\n }\n\n ListNode* temp = head;\n int cnt = 0;\n while(temp!=NULL) {\n cnt++;\n temp = temp->next;\n }\n\n temp = head;\n vector<int>arr(cnt);\n for(int i = 0; i<cnt; i++) {\n arr[i] = temp->val;\n temp = temp->next;\n }\n temp = head;\n sort(arr.begin(), arr.end());\n for(int i = 0; i<arr.size(); i++) {\n temp->val = arr[i];\n temp = temp->next;\n }\n\n\n return head;\n \n \n }\n};",
"memory": "22023"
} |
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": "class Solution {\npublic:\n ListNode* merge(ListNode* left, ListNode* right)\n {\n if (!left) return right;\n if (!right) return left;\n \n ListNode root;\n ListNode* head = &root;\n auto process = [&](ListNode ** curr) {\n head->next = *curr;\n *curr = (*curr)->next;\n head = head->next;\n head->next = nullptr;\n };\n while(left && right) {\n if (left->val < right->val) {\n process(&left);\n }\n else\n {\n process(&right);\n }\n }\n if (left) head->next = left;\n if (right) head->next = right;\n\n return root.next;\n }\n\n ListNode* sortList(ListNode* head) {\n \n // null or only one elements return directly\n if (!head)\n return nullptr;\n if(!head->next)\n return head;\n \n ListNode* slow = head;\n ListNode* prev = slow;\n ListNode* fast = head;\n\n while (fast) {\n prev = slow;\n slow = slow->next;\n fast = fast->next;\n if (!fast)\n break;\n else\n fast = fast->next;\n }\n prev->next = nullptr;\n ListNode* left = sortList(head);\n ListNode* right = sortList(slow);\n ListNode* root = merge(left, right);\n return root;\n }\n};",
"memory": "22966"
} |
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 == NULL || head->next == NULL){\n return head;\n }\n ListNode *slow = head, *fast = head;\n if(fast != NULL && fast->next != NULL && fast->next->next != NULL){\n fast = fast->next->next;\n slow = slow->next;\n }\n ListNode *head2 = slow->next;\n slow->next = NULL;\n return mergeList(sortList(head), sortList(head2));\n }\n\n ListNode *mergeList(ListNode *l1, ListNode *l2){\n if(l1 == NULL) return l2;\n if(l2 == NULL) return l1;\n ListNode *head = NULL;\n if(l1->val < l2->val){\n head = l1;\n l1 = l1->next;\n }else{\n head = l2;\n l2 = l2->next;\n }\n ListNode *travel = head;\n while(l1 != NULL && l2 != NULL){\n if(l1->val < l2->val){\n travel->next = l1;\n l1 = l1->next;\n }else{\n travel->next = l2;\n l2 = l2->next;\n }\n travel = travel->next;\n }\n if(l1 != NULL){\n travel->next = l1;\n }\n if(l2 != NULL){\n travel->next = l2;\n }\n return head;\n }\n};",
"memory": "23908"
} |
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 == NULL || head->next == NULL){\n return head;\n }\n ListNode *slow = head, *fast = head;\n if(fast != NULL && fast->next != NULL && fast->next->next != NULL){\n fast = fast->next->next;\n slow = slow->next;\n }\n ListNode *head2 = slow->next;\n slow->next = NULL;\n return mergeList(sortList(head), sortList(head2));\n }\n\n ListNode *mergeList(ListNode *l1, ListNode *l2){\n if(l1 == NULL) return l2;\n if(l2 == NULL) return l1;\n ListNode *head = NULL;\n if(l1->val < l2->val){\n head = l1;\n l1 = l1->next;\n }else{\n head = l2;\n l2 = l2->next;\n }\n ListNode *travel = head;\n while(l1 != NULL && l2 != NULL){\n if(l1->val < l2->val){\n travel->next = l1;\n l1 = l1->next;\n }else{\n travel->next = l2;\n l2 = l2->next;\n }\n travel = travel->next;\n }\n if(l1 != NULL){\n travel->next = l1;\n }\n if(l2 != NULL){\n travel->next = l2;\n }\n return head;\n }\n};",
"memory": "23908"
} |
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* findMid(ListNode* head){\n ListNode* slow = head;\n ListNode* fast = head->next;\n while(fast!=NULL && fast->next != NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n}\n// ListNode* merge(ListNode* &left, ListNode* &right){\n// ListNode* p1 =left;\n// ListNode* p2 =right;\n\n// ListNode* dummyNode = new ListNode(-1);\n// ListNode* p3 = dummyNode;\n\n// while(p1!=NULL && p2!=NULL){\n// if(p1->val < p2->val){\n// p3->next = p1;\n// p1 = p1->next;\n// }\n// else{\n// p3->next = p2;\n// p2 = p2->next;\n// }\n// p3 = p3->next;\n// }\n// while(p1!=NULL){\n// p3->next = p1;\n// p1 = p1->next;\n// p3 = p3->next;\n// }\n// while(p2!=NULL){\n// p3->next = p2;\n// p2 = p2->next;\n// p3 = p3->next;\n// }\n// return dummyNode->next;\n// }\n ListNode* merge(ListNode* &left, ListNode* &right){\n //base case\n if(left==NULL){\n return right;\n }\n if(right == NULL){\n return left;\n }\n ListNode* result;\n if(left->val < right ->val){\n result = left;\n result->next = merge(left->next, right);\n }\n else{\n result = right;\n result->next = merge(left, right->next);\n }\n return result;\n}\n ListNode* sortList(ListNode* head) {\n //USING MERGE SORT\n if(head == 0 || head->next == 0){\n return head;\n }\n ListNode* mid = findMid(head);\n ListNode* left = head;\n ListNode* right = mid->next;\n mid->next = NULL;\n\n //sort RE\n left = sortList(left);\n right = sortList(right);\n\n //merge both left and right LLS\n ListNode* mergedLL = merge(left, right);\n return mergedLL;\n }\n};",
"memory": "24851"
} |
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* findMid(ListNode* head){\n ListNode* slow = head;\n ListNode* fast = head->next;\n while(fast!=NULL && fast->next != NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n}\n// ListNode* merge(ListNode* &left, ListNode* &right){\n// ListNode* p1 =left;\n// ListNode* p2 =right;\n\n// ListNode* dummyNode = new ListNode(-1);\n// ListNode* p3 = dummyNode;\n\n// while(p1!=NULL && p2!=NULL){\n// if(p1->val < p2->val){\n// p3->next = p1;\n// p1 = p1->next;\n// }\n// else{\n// p3->next = p2;\n// p2 = p2->next;\n// }\n// p3 = p3->next;\n// }\n// while(p1!=NULL){\n// p3->next = p1;\n// p1 = p1->next;\n// p3 = p3->next;\n// }\n// while(p2!=NULL){\n// p3->next = p2;\n// p2 = p2->next;\n// p3 = p3->next;\n// }\n// return dummyNode->next;\n// }\n ListNode* merge(ListNode* &left, ListNode* &right){\n //base case\n if(left==NULL){\n return right;\n }\n if(right == NULL){\n return left;\n }\n ListNode* result;\n if(left->val < right ->val){\n result = left;\n result->next = merge(left->next, right);\n }\n else{\n result = right;\n result->next = merge(left, right->next);\n }\n return result;\n}\n ListNode* sortList(ListNode* head) {\n //USING MERGE SORT\n if(head == 0 || head->next == 0){\n return head;\n }\n ListNode* mid = findMid(head);\n ListNode* left = head;\n ListNode* right = mid->next;\n mid->next = NULL;\n\n //sort RE\n left = sortList(left);\n right = sortList(right);\n\n //merge both left and right LLS\n ListNode* mergedLL = merge(left, right);\n return mergedLL;\n }\n};",
"memory": "25793"
} |
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 */\n\nListNode* insert(ListNode* node, ListNode* list){\n if(!list){\n return node;\n }\n if(node->val<list->val){\n node->next=list;\n return node;\n }\n ListNode* itr = list;\n while(itr&&itr->next&&itr->next->val<node->val){\n itr=itr->next;\n }\n ListNode* temp = itr->next;\n itr->next=node;\n node->next=temp;\n return list;\n}\n\nclass Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n if(!head){\n return head;\n }\n ListNode* sortedList = sortList(head->next);\n return insert(head,sortedList);\n }\n};",
"memory": "26736"
} |
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 * merge( ListNode *&a, ListNode *&b){\n if( a == NULL)\n return b;\n\n else if(b == NULL)\n return a;\n\n ListNode*c = NULL;\n \n if( a->val < b->val){\n c = a;\n c->next = merge(a->next, b);\n }\n else{\n c = b;\n c->next= merge(a,b->next);\n }\n\n return c;\n }\n ListNode* mergeSort(ListNode* head) {\n if(!head || !head->next)\n return head;\n \n ListNode* slow = head;\n ListNode* fast = head;\n ListNode* prev = nullptr;\n\n while(fast && fast->next) {\n prev = slow;\n slow = slow->next;\n fast = fast->next->next;\n }\n \n prev->next = nullptr;\n\n ListNode* h1 = mergeSort(head);\n ListNode* h2 = mergeSort(slow);\n\n ListNode* c = merge(h1, h2);\n\n return c;\n }\n ListNode* sortList(ListNode* head) {\n ListNode* nhead = mergeSort(head);\n \n return nhead;\n }\n};",
"memory": "27678"
} |
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>
| 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* sortList(ListNode* head) {\n vector<int>ans;\n ListNode*temp=head;\n \n while(temp!=NULL){\n ans.push_back(temp->val);\n temp=temp->next;\n }\n sort(ans.begin(),ans.end());\n int n=ans.size();\n int i=0;\n temp=head;\n while(i<n&& temp!=NULL){\n temp->val=ans[i];\n temp=temp->next;\n i++;\n }\n return head;\n }\n};",
"memory": "34276"
} |
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>
| 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\n ListNode* findMid(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast = head -> next;\n \n while(fast != NULL && fast -> next != NULL) {\n slow = slow -> next;\n fast = fast -> next -> next; \n }\n return slow;\n}\n\n ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {\n if(list1==NULL){\n return list2;\n }\n if(list2==NULL){\n return list1;\n }\n ListNode *result=NULL;\n if(list1->val<list2->val){\n result=list1;\n result->next= mergeTwoLists(list1->next,list2);\n }\n else{\n result=list2;\n result->next= mergeTwoLists(list1,list2->next);\n }\n return result;\n }\n \n \n ListNode* sortList(ListNode* head) {\n if(head==NULL || head->next==NULL){\n return head;\n }\n ListNode*left=head;\n ListNode*middle=findMid(head);\n ListNode*right=middle->next;\n middle->next=NULL;\n left=sortList(left);\n right=sortList(right);\n\n ListNode*ans=mergeTwoLists(left,right);\n return ans;\n }\n};",
"memory": "34276"
} |
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>
| 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 */\n //proper merge sort, find middle of the linked list, then merge two sorted linked list\nclass Solution {\npublic:\n ListNode* findMiddle(ListNode* head){\n ListNode* sp = head;\n ListNode* fp = head->next; //in even case, we need the first node\n\n while(fp && fp->next){\n fp = fp->next->next;\n sp = sp->next;\n }\n return sp;\n }\n\n ListNode* merge(ListNode* l1, ListNode* l2){\n if(l1==NULL){\n return l2;\n }\n if(l2==NULL){\n return l1;\n }\n if(l1->val<l2->val){\n l1->next = merge(l1->next, l2);\n return l1;\n }else{\n l2->next = merge(l1, l2->next);\n return l2;\n }\n }\n ListNode* sortList(ListNode* head) {\n if(!head || !head->next){\n return head;\n }\n ListNode* middle = findMiddle(head);\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 merge(left, right);\n }\n};",
"memory": "35218"
} |
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>
| 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 void merge(vector<int>& arr, int s, int mid, int e) {\n int left = s;\n int right = mid + 1;\n int temp[e+1];\n int k = 0;\n while (left <= mid && right <= e) {\n if (arr[left] < arr[right]) {\n temp[k++] = arr[left++];\n } else {\n temp[k++] = arr[right++];\n }\n }\n while (left <= mid) {\n temp[k++] = arr[left++];\n }\n while (right <= e) {\n temp[k++] = arr[right++];\n }\n // int r=0;\n for (int i = s; i <= e; i++) { // s-s=0, s+1-s=1\n arr[i] = temp[i - s];\n }\n }\n void mergeSort(vector<int>& arr, int s, int e) {\n if (s >= e) {\n return;\n }\n int mid = (s + e) / 2;\n mergeSort(arr, s, mid);\n mergeSort(arr, mid + 1, e);\n merge(arr, s, mid, e);\n }\n ListNode* sortList(ListNode* head) {\n vector<int> v;\n ListNode* temp = head;\n while(temp != NULL){\n v.push_back(temp->val);\n temp=temp->next;\n }\n mergeSort(v, 0 ,v.size()-1);\n ListNode* curr = head;\n int k=0;\n while(curr != NULL){\n curr->val = v[k++];\n curr=curr->next;\n }\n return head;\n \n }\n};",
"memory": "36161"
} |
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>
| 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 */\n\nvoid help(ListNode* head, ListNode*& t1, ListNode*& t2) {\n if (!head)\n return;\n ListNode *slow = head, *fast = head->next;\n while (fast != nullptr && fast->next != nullptr) {\n slow = slow->next;\n fast = fast->next->next;\n }\n t2 = slow->next;\n slow->next = nullptr;\n t1 = head;\n return;\n}\n\nListNode* help2(ListNode*& n1, ListNode*& n2) {\n if (!n1)\n return n2;\n if (!n2)\n return n1;\n ListNode* s;\n if (n1->val <= n2->val) {\n s = n1;\n s->next = help2(n1->next, n2);\n } else {\n s = n2;\n s->next = help2(n1, n2->next);\n }\n return s;\n}\n\nclass Solution {\npublic:\n ListNode* sortList(ListNode*& head) {\n if (!head || !head->next) return head;\n ListNode *t1, *t2;\n help(head, t1, t2);\n if (t1)\n cout << t1->val << \" t \";\n if (t2)\n cout << t2->val << \" t \";\n cout << endl;\n // while(t1) {\n // cout<<t1->val<<\" \";\n // t1 = t1->next;\n // }\n // cout<<endl;\n ListNode *sl1, *sl2;\n sl1 = sortList(t1);\n sl2 = sortList(t2);\n if (sl1)\n cout << sl1->val << \" s \";\n if (sl2)\n cout << sl2->val << \" s \";\n cout << endl;\n head = help2(sl1, sl2);\n return head;\n }\n};",
"memory": "37103"
} |
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>
| 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* sortList(ListNode* head) {\n if(head == NULL)return head;\n vector<int>temp;\n ListNode *curr = head;\n while(head){\n temp.push_back(head->val);\n head = head->next;\n }\n stable_sort(temp.begin(),temp.end());\n head = curr;\n for(int i:temp){\n curr->val = i;\n curr= curr->next;\n }\n return head;\n\n }\n};",
"memory": "38046"
} |
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>
| 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//brute force method\n ListNode* convertToList(ListNode* head, vector<int> a){\n ListNode* temp = head;\n for(int i = 0; i < a.size() ; i++){\n temp->val = a[i];\n temp = temp->next;\n }\n return head;\n \n }\n ListNode* sortList(ListNode* head) {\n \n vector<int> a;\n ListNode* temp = head;\n\n while(temp!=NULL){\n a.push_back(temp->val);\n temp = temp->next;\n }\n\n sort(a.begin(),a.end());\n\n return convertToList(head,a);\n \n }\n};",
"memory": "38988"
} |
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>
| 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 *findMid(ListNode *node){\n ListNode *slow=node,*fast=node;\n while(fast->next && fast->next->next){\n slow=slow->next;\n fast=fast->next->next;\n }\n return slow;\n }\n ListNode* merge(ListNode *node1,ListNode* node2){\n if(!node1){return node2;}\n if(!node2){return node1;}\n if(node1->val<=node2->val){\n node1->next=merge(node1->next,node2);\n return node1;\n }\n else{\n node2->next=merge(node1,node2->next);\n cout<<node2->val;\n return node2;\n }\n }\n ListNode* mergeSort(ListNode* node){\n if(!node || !node->next)return node;\n ListNode* mid=findMid(node);\n ListNode* right=mid->next;\n mid->next=NULL;\n ListNode *left=node;\n left=mergeSort(left);\n right=mergeSort(right);\n return merge(left,right);\n\n }\n ListNode* sortList(ListNode* head) {\n return mergeSort(head);\n }\n};",
"memory": "39931"
} |
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>
| 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//brute force method\n ListNode* convertToList(ListNode* head, vector<int> a){\n ListNode* temp = head;\n for(int i = 0; i < a.size() ; i++){\n temp->val = a[i];\n temp = temp->next;\n }\n return head;\n \n }\n ListNode* sortList(ListNode* head) {\n \n vector<int> a;\n ListNode* temp = head;\n\n while(temp!=NULL){\n a.push_back(temp->val);\n temp = temp->next;\n }\n\n sort(a.begin(),a.end());\n\n return convertToList(head,a);\n \n }\n};",
"memory": "40873"
} |
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>
| 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* sortList(ListNode* head) {\n if(head == nullptr) return head;\n std::vector<ListNode*> nodes { head };\n nodes.reserve(50000);\n head = head->next;\n while(head != nullptr) {\n nodes.emplace_back(head);\n head = head->next;\n }\n\n std::sort(nodes.begin(), nodes.end(), [](const ListNode* l1, const ListNode* l2) { return l1->val < l2->val; });\n for(auto i { 0 }; i < nodes.size() - 1; i++) {\n nodes.at(i)->next = nodes.at(i + 1);\n }\n nodes.at(nodes.size() - 1)->next = nullptr;\n return *nodes.begin();\n }\n};",
"memory": "41816"
} |
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>
| 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* sortList(ListNode* head) {\n if(head == NULL) return NULL;\n if(head->next == NULL) return head;\n\n ListNode* ptr = head;\n vector<int> arr;\n\n while(ptr != NULL) {\n arr.push_back(ptr->val);\n ptr = ptr->next;\n }\n sort(arr.begin(), arr.end());\n queue<int>q;\n for(auto x : arr) {\n q.push(x);\n }\n \n ListNode* trv = head;\n while(trv != NULL) {\n int node = q.front();\n q.pop();\n trv->val = node;\n trv = trv->next;\n }\n return head;\n }\n};",
"memory": "42758"
} |
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>
| 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* sortList(ListNode* head) {\n\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n \n if(head == NULL) return NULL;\n if(head->next == NULL) return head;\n\n ListNode* ptr = head;\n vector<int> arr;\n\n while(ptr != NULL) {\n arr.push_back(ptr->val);\n ptr = ptr->next;\n }\n sort(arr.begin(), arr.end());\n queue<int>q;\n for(auto x : arr) {\n q.push(x);\n }\n \n ListNode* trv = head;\n while(trv != NULL) {\n int node = q.front();\n q.pop();\n trv->val = node;\n trv = trv->next;\n }\n return head;\n }\n};",
"memory": "42758"
} |
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>
| 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* sortList(ListNode* head) {\n ListNode* temp = head;\n vector<int>v1;\n stack<int>st;\n while(temp!=NULL)\n {\n v1.push_back(temp->val);\n temp = temp->next;\n }\n sort(v1.begin(),v1.end());\n reverse(v1.begin(),v1.end());\n for(int i=0;i<v1.size();i++)\n {\n st.push(v1[i]);\n }\n temp = head;\n while(temp!=NULL)\n {\n temp->val = st.top();\n st.pop();\n temp = temp->next;\n }\n return head;\n }\n};",
"memory": "43701"
} |
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>
| 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 vector<int> ans(ListNode* head){\n if(!head)\n return {};\n else {\n vector<int> res = ans(head->next);\n res.push_back(head->val);\n return res;\n }\n }\n ListNode* sortList(ListNode* head) {\n vector<int> res = ans(head);\n sort(res.begin(),res.end());\n ListNode* p = head;\n for(auto v:res){\n head->val = v;\n head = head->next;\n }\n return p;\n }\n};",
"memory": "44643"
} |
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>
| 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* sortList(ListNode* head) {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n \n if (!head || !head->next)\n return head;\n\n auto compare = [](ListNode* a, ListNode* b) {\n return a->val > b->val;\n };\n\n priority_queue<ListNode*, vector<ListNode*>,decltype(compare)> pq(compare);\n ListNode* ptr = head;\n\n while(ptr) {\n pq.push(ptr);\n ptr = ptr->next;\n }\n\n head = pq.top();\n pq.pop();\n ptr = head;\n\n while (!pq.empty()) {\n ptr->next = pq.top();\n pq.pop();\n ptr = ptr->next; \n }\n\n ptr->next = nullptr;\n return head;\n }\n};",
"memory": "45586"
} |
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>
| 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* mergeLists(ListNode* first, ListNode* second) {\n if (first == nullptr)\n return second;\n if (second == nullptr)\n return first; \n if (first->val > second->val) {\n ListNode* tmp = first;\n first = second;\n second = tmp;\n }\n \n ListNode* next = first, *secondNext = second;\n while (next->next != nullptr && secondNext != nullptr)\n {\n if (next->next->val < secondNext->val)\n next = next->next;\n else {\n ListNode* tmp = next->next;\n next->next = secondNext;\n secondNext = secondNext->next;\n next->next->next = tmp;\n next = next->next;\n }\n }\n if (secondNext != nullptr)\n next->next = secondNext;\n return first;\n }\n\n struct Data {\n int number = 0;\n ListNode* node = nullptr;\n };\n\n ListNode* sortList(ListNode* head) {\n std::vector<Data> stack;\n stack.reserve(1000);\n while (head != nullptr)\n { \n int count = stack.size();\n if (count > 1)\n {\n Data& last = stack[count-1];\n Data& prev = stack[count-2];\n if (last.number == prev.number)\n {\n prev.node = mergeLists(prev.node, last.node);\n prev.number *= 2;\n stack.pop_back();\n }\n }\n if (head != nullptr) \n { \n ListNode* tmp = head->next;\n head->next = nullptr;\n Data d;\n d.number = 1;\n d.node = head; \n stack.push_back(d);\n head = tmp;\n }\n }\n\n while (stack.size() > 1)\n {\n int count = stack.size();\n Data& last = stack[count-1];\n Data& prev = stack[count-2]; \n prev.node = mergeLists(prev.node, last.node);\n prev.number *= 2;\n stack.pop_back();\n }\n if (stack.empty())\n return nullptr;\n return stack[0].node;\n }\n};",
"memory": "46528"
} |
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>
| 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* mergeLists(ListNode* first, ListNode* second) {\n if (first == nullptr)\n return second;\n if (second == nullptr)\n return first; \n if (first->val > second->val) {\n ListNode* tmp = first;\n first = second;\n second = tmp;\n }\n \n ListNode* next = first, *secondNext = second;\n while (next->next != nullptr && secondNext != nullptr)\n {\n if (next->next->val < secondNext->val)\n next = next->next;\n else {\n ListNode* tmp = next->next;\n next->next = secondNext;\n secondNext = secondNext->next;\n next->next->next = tmp;\n next = next->next;\n }\n }\n if (secondNext != nullptr)\n next->next = secondNext;\n return first;\n }\n\n struct Data {\n int number = 0;\n ListNode* node = nullptr;\n };\n\n ListNode* sortList(ListNode* head) {\n std::vector<Data> stack;\n //stack.reserve(1000);\n while (head != nullptr)\n { \n int count = stack.size();\n if (count > 1)\n {\n Data& last = stack[count-1];\n Data& prev = stack[count-2];\n if (last.number == prev.number)\n {\n prev.node = mergeLists(prev.node, last.node);\n prev.number *= 2;\n stack.pop_back();\n }\n }\n if (head != nullptr) \n { \n ListNode* tmp = head->next;\n head->next = nullptr;\n Data d;\n d.number = 1;\n d.node = head; \n stack.push_back(d);\n head = tmp;\n }\n }\n\n while (stack.size() > 1)\n {\n int count = stack.size();\n Data& last = stack[count-1];\n Data& prev = stack[count-2]; \n prev.node = mergeLists(prev.node, last.node);\n prev.number *= 2;\n stack.pop_back();\n }\n if (stack.empty())\n return nullptr;\n return stack[0].node;\n }\n};",
"memory": "46528"
} |
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>
| 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 {\n static bool comp(ListNode* L1, ListNode* L2){\n return L1->val < L2->val;\n }\npublic:\n ListNode* sortList(ListNode* head) {\n if(!head) return head;\n vector<ListNode*> nodes;\n while(head){\n nodes.push_back(head);\n head = head->next;\n }\n int n = nodes.size();\n sort(nodes.begin(), nodes.end(), comp);\n nodes[n - 1]->next = NULL;\n for(int i = 0; i < n - 1; i++){\n nodes[i]->next = nodes[i + 1];\n }\n return nodes[0];\n }\n};",
"memory": "47471"
} |
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>
| 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 myc\n{\n public:\n bool operator()(ListNode* l1,ListNode* l2)\n {\n return l1->val>l2->val;\n }\n};\n\n\nclass Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n if(head==nullptr) return head;\n priority_queue<ListNode*,vector<ListNode*>,myc> que;\n while(head!=NULL)\n {\n //cout<<head->val<<\" \";\n que.push(head);\n head = head->next;\n\n }\n ListNode* pre = new ListNode();\n ListNode* nex = pre;\n while(!que.empty())\n {\n nex->next = que.top();\n que.pop();\n nex = nex->next;\n }\n nex->next = NULL;\n return pre->next;\n\n }\n};",
"memory": "48413"
} |
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>
| 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* sortList(ListNode* head) {\n vector<ListNode*> vec;\n while(head){\n vec.push_back(head);\n head = head->next;\n }\n sort(vec.begin(), vec.end(), [&](ListNode* a, ListNode* b){\n return a->val > b->val;\n });\n \n ListNode* prev = nullptr;\n for(int i = 0; i < vec.size(); i++){\n head = vec[i];\n head->next = prev;\n prev = head;\n }\n return head;\n }\n};",
"memory": "48413"
} |
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>
| 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* sortList(ListNode* head) {\n if (!head) return nullptr;\n std::vector<ListNode*> nodes;\n while (head) {\n nodes.emplace_back(head);\n head = head->next;\n }\n std::sort(nodes.begin(), nodes.end(), [](ListNode* node1, ListNode* node2) {\n return node1->val < node2->val;\n });\n for (int i = 0; i < nodes.size() - 1; ++i) {\n nodes[i]->next = nodes[i + 1];\n }\n nodes[nodes.size() - 1]->next = nullptr;\n return nodes[0];\n }\n};",
"memory": "49356"
} |
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>
| 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* sortList(ListNode* head) {\n if(head == nullptr) return head;\n std::vector<ListNode*> nodes { head };\n nodes.reserve(5000);\n head = head->next;\n while(head != nullptr) {\n nodes.emplace_back(head);\n head = head->next;\n }\n\n std::sort(nodes.begin(), nodes.end(), [](const ListNode* l1, const ListNode* l2) { return l1->val < l2->val; });\n for(auto i { 0 }; i < nodes.size() - 1; i++) {\n nodes.at(i)->next = nodes.at(i + 1);\n }\n nodes.at(nodes.size() - 1)->next = nullptr;\n return *nodes.begin();\n }\n};",
"memory": "50298"
} |
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>
| 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* sortList(ListNode* head) {\n if (head == nullptr) {\n return head;\n }\n deque<ListNode*> dq;\n while (head != nullptr) {\n dq.push_back(head);\n head = head->next;\n }\n ranges::sort(dq, less<>{}, &ListNode::val);\n for (int i = 0; i < dq.size() - 1; i++) {\n dq[i]->next = dq[i + 1];\n }\n dq.back()->next = nullptr;\n return dq.front();\n }\n};",
"memory": "51241"
} |
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>
| 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* sortList(ListNode* head) {\n if (head == nullptr) {\n return head;\n }\n vector<int> nums;\n ListNode* node = head;\n while (node != nullptr) {\n nums.push_back(node->val);\n node = node->next;\n }\n\n quickSort(nums, 0, nums.size()-1);\n // sort(nums.begin(), nums.end());\n node = head;\n for (int i = 0; i < nums.size() && node != nullptr; i++) {\n node->val = nums[i];\n node = node->next;\n }\n\n return head;\n\n }\n\n void quickSort(vector<int>& nums, int start, int end) {\n if (start >= end ) {\n return;\n }\n int pivot = nums[end];\n int left = start, right = end;\n while (left <= right) {\n while (left <= right && nums[left] < pivot) {\n left++;\n }\n while (left <= right && nums[right] > pivot) {\n right--;\n }\n if (left <= right) {\n swap(nums[left], nums[right]);\n left++;\n right--;\n }\n }\n quickSort(nums, start, right);\n quickSort(nums, left, end);\n return;\n }\n};",
"memory": "57838"
} |
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>
| 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 */\n #define MAX_SIZE 50000\nclass Solution {\npublic:\n /*void printList(ListNode* head, int max){\n cout<<\"[\";\n int i=0;\n while(head &&i++<max)\n {\n cout<<head->val<<\",\";\n head=head->next;\n }\n cout<<\"]\"<<endl;\n }*/\n void printList(ListNode* head){\n cout<<\"[\";\n while(head)\n {\n cout<<head->val<<\",\";\n head=head->next;\n }\n cout<<\"]\"<<endl;\n }\n /*void qsortList(ListNode* head,int len)\n {\n //printList(head,maxLen);\n if(len==2)\n {\n if(head->next->val<head->val)\n {\n swap(head->next->val,head->val);\n }\n return;\n }\n ListNode* pivot=head;\n int i;\n for(i=0;i<len/2;i++)\n {\n pivot=pivot->next;\n }\n int pv=pivot->val;\n swap(pivot->val,head->val);\n //cout<<\"pv=\"<<pv<<\" pivot->val=\"<<pivot->val<<\" head->val=\"<<head->val<<endl;\n pivot=head;\n ListNode* cur=head;\n ListNode* prev=nullptr;\n i=-1;\n for(int j=0;j<len;j++)\n {\n if(cur->val<=pv)\n {\n prev=pivot;\n i++;\n if(i!=j)\n {\n swap(pivot->val,cur->val);\n }\n pivot=pivot->next;\n }\n cur=cur->next;\n }\n //cout<<\"i=\"<<i<<\" pivot->val=\"<<pivot->val<<endl;\n if(prev)\n {\n //cout<<\"prev-val=\"<<prev->val<<endl;\n swap(prev->val,head->val);\n }\n //printList(head);\n if(i>1)\n {\n qsortList(head,i);\n }\n if(len-i>2)\n {\n qsortList(pivot,len-i-1);\n }\n \n }*/\n ListNode* sortList(ListNode* head) {\n if(!head || !head->next)\n {\n return head;\n }\n int len=0;\n vector<int> nodes(MAX_SIZE);\n ListNode* cur=head;\n while(cur)\n {\n nodes[len]=cur->val;\n cur=cur->next;\n len++;\n }\n sort(nodes.begin(),nodes.begin()+len);\n cur=head;\n for(int i=0;i<len;i++)\n {\n cur->val=nodes[i];\n cur=cur->next;\n }\n //cout<<\"len=\"<<len<<endl;\n //qsortList(head,len);\n return head;\n }\n};",
"memory": "58781"
} |
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>
| 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* mergeTwo(ListNode* first, ListNode* second){\n if (!first) return second;\n if (!second) return first;\n if (first->val < second->val){\n first->next = mergeTwo(first->next, second);\n return first;\n } else{\n second->next = mergeTwo(first, second->next);\n return second;\n }\n }\n void mergeLists(vector<ListNode*>& v){\n if (v.size() == 0) return ;\n while (v.size() > 1){\n v.push_back(mergeTwo(v[0], v[1]));\n v.erase(v.begin());\n v.erase(v.begin());\n }\n }\n ListNode* sortList(ListNode* head) {\n if (!head) return NULL;\n vector<ListNode*> v;\n while (head){\n v.push_back(head);\n head = head -> next;\n v.back()->next = NULL;\n }\n mergeLists(v);\n return v.front();\n }\n};",
"memory": "59723"
} |
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>
| 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 *nextHead;\n// ListNode *prevEnd;\n ListNode *breakList(ListNode *head, int n, ListNode **nextHead) {\n ListNode *mid = head, *midPrev = head, *fast = head->next ? head->next : head;\n int i;\n for (i = 1; i < n && (midPrev->next || fast->next); i++) {\n midPrev = midPrev->next;\n if (fast && fast->next)\n fast = fast->next;\n if (fast->next) fast = fast->next;\n }\n mid = midPrev->next;\n midPrev->next = NULL;\n *nextHead = fast ? fast->next : NULL;\n if (fast) fast->next = NULL;\n return mid;\n }\n ListNode *mergeList(ListNode *l1, ListNode *l2, ListNode **prevEnd) {\n ListNode *dummy = new ListNode (-1);\n ListNode *cur = dummy;\n while (l1 && l2) {\n if (l1->val < l2->val) {\n cur->next = l1;\n l1 = l1->next;\n } else {\n cur->next = l2;\n l2 = l2->next;\n }\n cur = cur->next;\n *prevEnd = cur;\n }\n if (l1) cur->next = l1;\n if (l2) cur->next = l2;\n while (cur) {\n *prevEnd = cur;\n cur = cur->next;\n }\n return dummy->next;\n }\n \n ListNode* sortList(ListNode* head) {\n ListNode *dummy = new ListNode(-1, head);\n ListNode *cur = dummy->next;\n int n = 0;\n while (cur) {\n cur = cur->next;\n n++;\n }\n cur = dummy;\n ListNode *prevEnd = dummy;\n int len = 1;\n ListNode *l1Head = head, *l2Head = head;\n ListNode *nextHead;\n ListNode *temp = new ListNode(-1);\n // nextHead = &temp;\n while (len < n) {\n // cout << \"len: \" << len << \"\\n\";\n l1Head = dummy->next;\n cur = dummy;\n while (l1Head) {\n l2Head = breakList(l1Head, len, &nextHead);\n // cout << \"l1: \" << l1Head->val << \" \";\n // if (l2Head)\n // cout << \"l2: \" << l2Head->val << \"\\n\";\n cur->next = mergeList(l1Head, l2Head, &prevEnd);\n l1Head = nextHead;\n // if (nextHead)\n // cout << \"nextHead: \" << nextHead->val << \"\\n\";\n cur = prevEnd;\n }\n // print(dummy->next);\n len *= 2;\n }\n return dummy->next;\n }\n};",
"memory": "60666"
} |
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>
| 1 | {
"code": "\nclass Solution {\npublic:\n // Merge two sorted linked lists\n ListNode* merge(ListNode* l1, ListNode* l2) {\n ListNode* dummy = new ListNode();\n ListNode* temp = dummy;\n \n // Merge two lists\n while (l1 != nullptr && l2 != nullptr) {\n if (l1->val < l2->val) {\n temp->next = l1;\n l1 = l1->next;\n } else {\n temp->next = l2;\n l2 = l2->next;\n }\n temp = temp->next;\n }\n\n // If any elements remain in either list\n if (l1 != nullptr) temp->next = l1;\n else temp->next = l2;\n\n return dummy->next;\n }\n\n // Split the list into two lists of size `size`, return the next sublist to process\n ListNode* split(ListNode* head, int size) {\n for (int i = 1; head != nullptr && i < size; i++) {\n head = head->next;\n }\n if (head == nullptr) return nullptr;\n\n ListNode* second = head->next;\n head->next = nullptr; // Break the list\n\n return second; // Return the starting point of the next sublist\n }\n\n // Main iterative merge sort function\n ListNode* sortList(ListNode* head) {\n if (!head || !head->next) return head; // Base case: empty or single-node list\n \n // Step 1: Get the length of the list\n ListNode* curr = head;\n int length = 0;\n while (curr != nullptr) {\n length++;\n curr = curr->next;\n }\n\n ListNode* dummy = new ListNode(0);\n dummy->next = head;\n ListNode* left;\n ListNode* right;\n ListNode* tail;\n\n // Step 2: Iteratively merge sublists\n for (int size = 1; size < length; size *= 2) {\n curr = dummy->next;\n tail = dummy;\n\n while (curr != nullptr) {\n left = curr;\n right = split(left, size); // Split the list into two halves\n curr = split(right, size); // Get the next sublist\n\n // Merge two halves and attach to tail\n tail->next = merge(left, right);\n\n // Move tail to the end of the merged list\n while (tail->next != nullptr) {\n tail = tail->next;\n }\n }\n }\n\n return dummy->next;\n }\n};\n",
"memory": "61608"
} |
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>
| 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 *tail, *nextSubList;\n\n ListNode* sortList(ListNode* head) {\n if (head == nullptr || head->next == nullptr) return head;\n int n = getSize(head);\n ListNode *sentinel = new ListNode(0, head);\n for (int size = 1; size < n; size *= 2) {\n ListNode *start = sentinel->next;\n tail = sentinel;\n while (start != nullptr) {\n if (start->next == nullptr) {\n tail->next = start;\n break;\n }\n ListNode *mid = getMid(start, size);\n merge(start, mid);\n start = nextSubList;\n }\n }\n return sentinel->next;\n }\n\n int getSize(ListNode* head) {\n int n = 0;\n while (head != nullptr) {\n n++;\n head = head->next;\n }\n return n;\n }\n\n ListNode* getMid(ListNode* start, int size) {\n ListNode *midPrev = start;\n ListNode *end = start->next;\n for (int i = 1; i < size && midPrev->next != nullptr; i++) {\n midPrev = midPrev->next;\n if (end->next != nullptr) {\n end = (end->next->next != nullptr) ? end->next->next : end->next;\n }\n }\n ListNode *mid = midPrev->next;\n nextSubList = end->next;\n midPrev->next = nullptr;\n end->next = nullptr;\n return mid;\n }\n\n void merge(ListNode* l1, ListNode* l2) {\n ListNode *sentinel = new ListNode(), *curr = sentinel;\n while (l1 != nullptr && l2 != nullptr) {\n if (l1->val < l2->val) {\n curr->next = l1;\n l1 = l1->next;\n } else {\n curr->next = l2;\n l2 = l2->next;\n }\n curr = curr->next;\n }\n curr->next = (l1 != nullptr) ? l1 : l2;\n\n while (curr->next != nullptr) curr = curr->next;\n\n tail->next = sentinel->next;\n tail = curr;\n }\n};",
"memory": "62551"
} |
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>
| 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:\nListNode *nextHead;\nListNode *prevEnd;\n ListNode *breakList(ListNode *head, int n) {\n ListNode *mid = head, *midPrev = head, *fast = head->next ? head->next : head;\n int i;\n for (i = 1; i < n && (midPrev->next || fast->next); i++) {\n //if (midPrev->next)\n midPrev = midPrev->next;\n //else break;\n if (fast && fast->next)\n fast = fast->next;\n //else break;\n if (fast->next) fast = fast->next;\n //else break;\n }\n mid = midPrev->next;\n midPrev->next = NULL;\n nextHead = fast ? fast->next : NULL;\n if (fast) fast->next = NULL;\n return mid;\n }\n ListNode *mergeList(ListNode *l1, ListNode *l2) {\n ListNode *dummy = new ListNode (-1);\n ListNode *cur = dummy;\n while (l1 && l2) {\n if (l1->val < l2->val) {\n cur->next = l1;\n l1 = l1->next;\n } else {\n cur->next = l2;\n l2 = l2->next;\n }\n cur = cur->next;\n prevEnd = cur;\n }\n if (l1) cur->next = l1;\n if (l2) cur->next = l2;\n while (cur) {\n prevEnd = cur;\n cur = cur->next;\n }\n return dummy->next;\n }\n \n ListNode* sortList(ListNode* head) {\n ListNode *dummy = new ListNode(-1, head);\n ListNode *cur = dummy->next;\n int n = 0;\n while (cur) {\n cur = cur->next;\n n++;\n }\n cur = dummy;\n prevEnd = dummy;\n int len = 1;\n ListNode *l1Head = head, *l2Head = head;\n while (len < n) {\n // cout << \"len: \" << len << \"\\n\";\n l1Head = dummy->next;\n cur = dummy;\n while (l1Head) {\n l2Head = breakList(l1Head, len);\n // cout << \"l1: \" << l1Head->val << \" \";\n // if (l2Head)\n // cout << \"l2: \" << l2Head->val << \"\\n\";\n cur->next = mergeList(l1Head, l2Head);\n l1Head = nextHead;\n // if (nextHead)\n // cout << \"nextHead: \" << nextHead->val << \"\\n\";\n cur = prevEnd;\n }\n // print(dummy->next);\n len *= 2;\n }\n return dummy->next;\n }\n};",
"memory": "62551"
} |
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>
| 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 */\n\nconst int N = 5e4 + 2;\nint a[N];\n\nint partition(int low, int high) {\n int i = low - 1;\n int pivot_index = low + rand() % (high - low + 1);\n swap(a[pivot_index], a[high]);\n int pivot = a[high];\n for(int j = low;j <= high;j++) {\n if(a[j] <= pivot) {\n swap(a[++i], a[j]);\n }\n }\n return i;\n}\n\nvoid quicksort(int low, int high) {\n if(low >= high)\n return;\n int mid = partition(low, high);\n quicksort(low, mid - 1);\n quicksort(mid + 1, high);\n}\n\nclass Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n if (head == nullptr)\n return nullptr;\n int n = 0;\n for(auto node = head;node;node = node->next) {\n a[++n] = node->val;\n }\n quicksort(1, n);\n ListNode* newHead = new ListNode(a[1]);\n ListNode* tail = newHead;\n for(int i = 2;i <= n;i++) {\n tail->next = new ListNode(a[i]);\n tail = tail->next;\n }\n return newHead;\n }\n};",
"memory": "63493"
} |
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>
| 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 */\n\ntypedef pair<ListNode*,ListNode*> LinkedList;\n\nclass Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n if(head == nullptr)\n return nullptr;\n \n int sort_len = 1;\n LinkedList sorted_part = sort_first_k(head, sort_len);\n while(sorted_part.second) {\n LinkedList the_other_sorted_part = sort_first_k(sorted_part.second, sort_len);\n ListNode* merge_head = merge(sorted_part.first, the_other_sorted_part.first);\n sorted_part = LinkedList( merge_head, the_other_sorted_part.second );\n sort_len += 1;\n }\n return sorted_part.first;\n }\n\n LinkedList sort_first_k(ListNode* head, int k) {\n \n // the list \"head\" is guaranteed to be non-empty\n if(k == 1) {\n LinkedList ans{head, head->next};\n head->next = nullptr; // make sure a list is ended with nullptr\n return ans;\n }\n\n // int left_len = (k+1)/2;\n // int right_len = k - left_len;\n LinkedList left_part = sort_first_k(head, (k +1) / 2);\n if(left_part.second == nullptr)\n return left_part;\n else {\n LinkedList right_part = sort_first_k(left_part.second, k - (k+1)/2);\n return LinkedList( merge(left_part.first, right_part.first), right_part.second );\n }\n }\n\n ListNode* merge(ListNode* ptr_1, ListNode* ptr_2) {\n // the end of list is indicated by nullptr\n ListNode* prehead = new ListNode();\n ListNode* ptr = prehead;\n\n while(ptr_1 != nullptr && ptr_2 != nullptr) {\n // ensure stability\n if(ptr_1->val <= ptr_2->val) {\n ptr->next = ptr_1;\n ptr = ptr->next;\n ptr_1 = ptr_1->next;\n } else {\n ptr->next = ptr_2;\n ptr = ptr->next;\n ptr_2 = ptr_2->next;\n }\n }\n\n if(ptr_1 == nullptr) {\n ptr->next = ptr_2;\n } else {\n ptr->next = ptr_1;\n }\n\n return prehead->next;\n }\n};",
"memory": "63493"
} |
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>
| 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 */\n\ntypedef pair<ListNode*,ListNode*> LinkedList;\n\nclass Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n if(head == nullptr)\n return nullptr;\n \n int sort_len = 1;\n LinkedList sorted_part = sort_first_k(head, sort_len);\n while(sorted_part.second) {\n LinkedList the_other_sorted_part = sort_first_k(sorted_part.second, sort_len);\n ListNode* merge_head = merge(sorted_part.first, the_other_sorted_part.first);\n sorted_part = LinkedList( merge_head, the_other_sorted_part.second );\n sort_len += 1;\n }\n return sorted_part.first;\n }\n\n LinkedList sort_first_k(ListNode* head, int k) {\n \n // the list \"head\" is guaranteed to be non-empty\n if(k == 1) {\n LinkedList ans{head, head->next};\n head->next = nullptr; // make sure a list is ended with nullptr\n return ans;\n }\n\n LinkedList left_part = sort_first_k(head, (k +1) / 2);\n if(left_part.second == nullptr)\n return left_part;\n else {\n LinkedList right_part = sort_first_k(left_part.second, k - (k+1)/2);\n return LinkedList( merge(left_part.first, right_part.first), right_part.second );\n }\n }\n\n ListNode* merge(ListNode* ptr_1, ListNode* ptr_2) {\n // the end of list is indicated by nullptr\n ListNode* prehead = new ListNode();\n ListNode* ptr = prehead;\n\n while(ptr_1 != nullptr && ptr_2 != nullptr) {\n // ensure stability\n if(ptr_1->val <= ptr_2->val) {\n ptr->next = ptr_1;\n ptr = ptr->next;\n ptr_1 = ptr_1->next;\n } else {\n ptr->next = ptr_2;\n ptr = ptr->next;\n ptr_2 = ptr_2->next;\n }\n }\n\n if(ptr_1 == nullptr) {\n ptr->next = ptr_2;\n } else {\n ptr->next = ptr_1;\n }\n\n return prehead->next;\n }\n};",
"memory": "64436"
} |
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>
| 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* sortList(ListNode* head) {\n if (head == nullptr) return head;\n auto cmp = [](pair<int, ListNode*>& o, pair<int, ListNode*>& t) {\n return o.first > t.first;\n };\n priority_queue<pair<int, ListNode*>, vector<pair<int, ListNode*>>, decltype(cmp)> q(cmp);\n while (head != nullptr) {\n q.push({head->val, head});\n head = head->next;\n }\n\n head = q.top().second;\n q.pop();\n ListNode* curNode = head;\n\n while (curNode != nullptr) {\n curNode->next = q.top().second;\n q.pop();\n curNode->next->next = nullptr;\n curNode = curNode->next;\n }\n return head;\n }\n};",
"memory": "64436"
} |
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>
| 1 | {
"code": "class Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n if (head == nullptr) {\n return nullptr;\n }\n if (head->next == nullptr) {\n return head;\n }\n\n // Extract the elements from the linked list\n std::vector<int> elements;\n ListNode* current = head;\n while (current != nullptr) {\n elements.push_back(current->val);\n current = current->next;\n }\n\n // Sort the elements\n std::sort(elements.begin(), elements.end());\n\n // Create a new sorted linked list\n ListNode* result = new ListNode(elements[0]);\n ListNode* resultPointer = result;\n for (size_t i = 1; i < elements.size(); ++i) {\n resultPointer->next = new ListNode(elements[i]);\n resultPointer = resultPointer->next;\n }\n\n return result;\n }\n};",
"memory": "65378"
} |
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>
| 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* sortList(ListNode* head) {\n vector<int> ans;\n while(head){\n ans.push_back(head->val);\n head = head->next;\n }\n sort(ans.begin(),ans.end());\n ListNode* nums = new ListNode(0);\n ListNode* temp = nums;\n for(auto i:ans){\n nums->next = new ListNode(i);\n nums = nums->next;\n }\n return temp->next;\n }\n};",
"memory": "65378"
} |
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>
| 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:\nListNode* sortedList(ListNode* &head1,vector<int> v)\n{\n int count = 0;\n ListNode* temp = head1;\n while(count != v.size())\n {\n temp -> next = new ListNode(v[count++]);\n temp = temp -> next;\n }\n return head1->next;\n}\n ListNode* sortList(ListNode* head) {\n ListNode* temp = head;\n vector<int> v;\n while(temp != NULL)\n {\n v.push_back(temp -> val);\n temp = temp -> next; \n }\n sort(v.begin(),v.end());\n\n ListNode* dummy = new ListNode(0);\n ListNode* head1 = dummy;\n\n ListNode* ans = sortedList(head1,v);\n return ans;\n }\n};\n\n\n\n\n// ListNode* curr1 = list1;\n// ListNode* curr2 = list2;\n\n// ListNode ans = new ListNode;\n\n// while(curr1 != NULL && curr2 != NULL)\n// {\n// if(curr1 -> val <= curr2 -> val)\n// {\n// ans -> next = curr1;\n// curr1 = curr1 -> next;\n// }\n// else{\n// ans -> next = curr2;\n// curr2 = curr2 -> next;\n// }\n// ans = ans -> next;\n// }\n// if(curr1 == NULL)\n// {\n// ans -> next = curr2;\n// }\n// else if(curr2 == NULL)\n// {\n// ans -> next = curr1;\n// }\n// return ans;",
"memory": "66321"
} |
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>
| 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 {\n ListNode* insert(ListNode* sorted, ListNode* node){\n if(node->val <= sorted->val){\n node->next = sorted;\n return node;\n }\n ListNode* temp = sorted;\n while(temp && temp->next){\n if(temp->next->val >= node->val){\n node->next = temp->next;\n temp->next = node;\n return sorted;\n }\n temp = temp->next;\n }\n temp->next = node;\n return sorted;\n }\npublic:\n ListNode* sortList(ListNode* head) {\n if(head == nullptr || head->next == nullptr) return head;\n\n ListNode* node = new ListNode(head->val);\n ListNode* sorted = sortList(head->next);\n ListNode* result = insert(sorted, node);\n\n return result;\n }\n};",
"memory": "66321"
} |
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>
| 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* sortList(ListNode* head) {\n ListNode* curr = head;\n vector<int>nums;\n if(head == NULL){\n return NULL;\n }\n while(curr != NULL){\n nums.push_back(curr->val);\n ListNode* temp = curr;\n curr = curr -> next;\n delete temp;\n }\n sort(nums.begin(),nums.end());\n ListNode* head2 = new ListNode(nums[0]);\n ListNode* curr2 = head2;\n for(int i = 1; i < nums.size(); i++){\n curr2 -> next = new ListNode(nums[i]);\n curr2 = curr2 -> next;\n }\n\n return head2;\n\n }\n};",
"memory": "67263"
} |
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>
| 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 */\n class Solution {\n public:\n ListNode* sortList(ListNode* head) {\n ListNode* curr=head;\n vector<int> val;\n while(curr){\n val.push_back(curr->val);\n ListNode* temp=curr;\n curr=curr->next;\n delete temp;\n }\n sort(val.begin(),val.end());\n\n ListNode* heads=NULL;\n ListNode* prev=NULL;\n for(auto i : val){\n\n ListNode* temp=new ListNode(i);\n if(heads==NULL){\n heads=temp;\n prev=temp;\n }else{\n prev->next=temp;\n prev=temp;\n }\n\n }\n \n\n\n\n return heads;\n\n\n }\n };",
"memory": "67263"
} |
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>
| 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 */\n #include <algorithm>\nclass Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n std::list<int> l;\n for (ListNode* x = head; x != nullptr; x = x->next) {\n l.push_back(x->val);\n }\n l.sort();\n for (ListNode* x = head; x != nullptr; x = x->next) {\n x->val = l.front();\n l.pop_front();\n }\n return head;\n }\n};\n",
"memory": "68206"
} |
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>
| 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 vector<int> convert(ListNode* &head){\n \n ListNode* temp = head;\n vector<int> res;\n\n while(temp != NULL){\n res.push_back(temp->val);\n temp = temp->next;\n }\n\n return res;\n } \n ListNode* convertll(vector<int> &arr,int idx){\n\n if(idx >= arr.size()){\n return NULL;\n }\n \n int data = arr[idx];\n ListNode* temp = new ListNode(data);\n\n temp->next = convertll(arr,idx+1);\n\n return temp;\n }\n ListNode* sortList(ListNode* head) {\n \n if(head == NULL || head->next == NULL){\n return head;\n }\n vector<int> ans = convert(head);\n\n sort(ans.begin(),ans.end());\n\n ListNode* curr = convertll(ans,0);\n\n return curr; \n }\n};",
"memory": "68206"
} |
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>
| 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* sortList(ListNode* head) {\n if(head == NULL || head->next == NULL) return head;\n map<int, int> mpp;\n ListNode* temp = head;\n\n //Storing each element to ordered hash map\n while(temp) {\n mpp[temp->val]++;\n\n temp = temp->next;\n }\n temp = head;\n for(auto it : mpp) {\n // cout<<it.first<<\" \";\n for(int i=0; i<it.second; i++) {\n temp->val = it.first;\n temp = temp->next;\n }\n }\n return head;\n }\n};",
"memory": "69148"
} |
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>
| 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* sortList(ListNode* head) {\n ListNode *p=head;\n map<int,int>mp;\n while(p!=nullptr)\n {\n mp[p->val]++;\n p=p->next;\n }\n p=head;\n for(auto it=mp.begin();it!=mp.end();it++)\n {\n //if(it.second>0)\n //{\n while(it->second--)\n {\n p->val=it->first;\n p=p->next;\n }\n //}\n }\n return head;\n }\n};",
"memory": "69148"
} |
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>
| 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\n ListNode* findMid(ListNode* head){\n ListNode* slow = head, *fast = head;\n while(fast && fast->next){\n slow = slow->next;\n fast = fast->next->next;\n } \n return slow;\n }\n\n ListNode* mergeTwoLists(ListNode* l1, ListNode* l2){\n if(l1==NULL && l2==NULL)\n return NULL;\n ListNode *temp1 = l1, *temp2 = l2;\n ListNode *dummy = new ListNode(-1);\n ListNode *temp = dummy;\n while(temp1 && temp2){\n if(temp1->val < temp2->val){\n temp->next = temp1;\n temp = temp1;\n temp1 = temp1->next;\n }\n else{\n temp->next = temp2;\n temp = temp2;\n temp2 = temp2->next;\n }\n }\n if(temp1){\n temp->next = temp1;\n }\n if(temp2){\n temp->next = temp2;\n }\n return dummy->next;\n }\n\n ListNode* sortList(ListNode* head) {\n if(head==NULL || head->next==NULL)\n return head;\n if(head->next->next==NULL){\n if(head->val < head->next->val)\n return head;\n else{\n ListNode* front = head->next;\n front->next = head;\n head->next = NULL;\n return front;\n }\n }\n ListNode* mid = findMid(head);\n ListNode* right = mid->next;\n mid->next = NULL;\n ListNode* left = sortList(head);\n right = sortList(right);\n return mergeTwoLists(left, right);\n }\n};",
"memory": "70091"
} |
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>
| 1 | {
"code": "/*\nOriginal solution.\n*/\n\n#define HEAD (0)\n\ntypedef ListNode Node;\n\nclass Solution {\npublic:\n static bool comparator(Node* a, Node* b){\n /*\n NOTE:\n - We have a comparator to use the in-built sort function.\n */\n return a->val < b->val;\n }\n\n Node* sortList(Node* head) {\n /*\n NOTE:\n - If the list is empty then we return `NULL`.\n */\n if(!head){\n return NULL;\n }\n\n /*\n NOTE:\n - Converting a conventional list to a vector array so that we can \n sort it using an implementation of mergesort. \n */\n vector<Node*> converted;\n this->getList(head, converted);\n\n sort(converted.begin(), converted.end(), this->comparator);\n\n /*\n NOTE:\n - Reattaching the list. \n */\n for(int i = 0, j = 1; j < converted.size(); i++, j++){\n converted[i]->next = converted[j];\n }\n converted[converted.size() - 1]->next = NULL;\n\n return converted[HEAD];\n }\n\n void getList(Node* head, vector<Node*>& list){\n /*\n NOTE:\n - Recursive function to get a conventional singly linked and convert\n it to an array. Technically not converting, just modifying the empty\n array passed into it.\n */\n if(!head){\n return;\n }\n\n list.push_back(head);\n\n getList(head->next, list);\n }\n};",
"memory": "70091"
} |
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>
| 1 | {
"code": "/*\nOriginal solution. JV.\n*/\n\ntypedef ListNode Node;\n\nclass Solution {\npublic:\n static bool comparator(Node* a, Node* b){\n return a->val < b->val;\n }\n\n Node* sortList(Node* head) {\n if(!head){\n return NULL;\n }\n\n vector<Node*> converted;\n\n this->getList(head, converted);\n\n sort(converted.begin(), converted.end(), this->comparator);\n\n for(int i = 0, j = 1; j < converted.size(); i++, j++){\n converted[i]->next = converted[j];\n }\n\n converted[converted.size() - 1]->next = NULL;\n\n return converted[0];\n }\n\n void getList(Node* head, vector<Node*>& list){\n if(!head){\n return;\n }\n\n list.push_back(head);\n\n getList(head->next, list);\n }\n};",
"memory": "71033"
} |
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>
| 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* sortList(ListNode* head) {\n ListNode*p=head;\n set<int>st;\n vector<int>v;\n while(p){\n v.push_back(p->val);\n st.insert(p->val);\n p=p->next;\n\n }\n sort(v.begin(),v.end());\n p=head;\n for(int i=0;i<v.size();i++){\n p->val=v[i];\n p=p->next;\n }\n return head;\n\n\n\n \n }\n};",
"memory": "71033"
} |
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>
| 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 */\n#include <set>\n\nstruct ListNodeComparator {\n bool operator()(const ListNode* lhs, const ListNode* rhs) const {\n return lhs->val < rhs->val;\n }\n};\n\n\n\nclass Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n \n std::multiset<ListNode*, ListNodeComparator> custom_set;\n auto curr = head;\n while(curr)\n {\n custom_set.insert(curr);\n curr = curr->next;\n }\n ListNode dummy(0, nullptr);\n curr = &dummy;\n for(auto it = custom_set.begin(); it != custom_set.end(); ++it)\n {\n curr->next = *it;\n curr = *it;\n }\n curr->next = nullptr;\n\n return dummy.next;\n\n if(!head) return head;\n\n auto outer = head;\n while(outer)\n {\n auto inner = head;\n while(inner && inner != outer)\n {\n if(outer->val < inner->val)\n {\n std::swap(outer->val, inner->val);\n }\n inner = inner->next;\n }\n outer = outer->next;\n\n }\n return head;\n }\n};",
"memory": "71976"
} |
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>
| 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* sortList(ListNode* head) {\n if (!head || !head->next) return head;\n unordered_map<ListNode*, int> map;\n ListNode* curr = head;\n while (curr) {\n map[curr] = curr->val;\n curr = curr->next;\n }\n vector<pair<ListNode*, int>> vec(map.begin(), map.end());\n sort(vec.begin(), vec.end(), [](pair<ListNode*, int>& a, pair<ListNode*, int>& b) {\n return a.second < b.second;\n });\n head = vec[0].first;\n curr = head;\n for (int i = 1; i < vec.size(); i++) {\n curr->next = vec[i].first;\n curr = curr->next;\n }\n curr->next = nullptr;\n return head;\n }\n};",
"memory": "77631"
} |
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>
| 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* sortList(ListNode* head) {\n unordered_map<ListNode*,int> map;\n if(head == NULL || head->next == NULL) return head;\n \n ListNode *curr = head;\n ListNode *dummy;\n while(curr != NULL)\n {\n dummy = curr->next;\n curr->next = NULL;\n map[curr] = curr->val;\n curr = dummy;\n \n }\n vector<pair<ListNode*, int>> v(map.begin(), map.end());\n sort (v.begin(), v.end(), [&v] (pair<ListNode*, int>a, pair<ListNode*, int>b) {\n return a.second<b.second;\n });\n ListNode *temp = new ListNode();\n ListNode *head1 = temp;\n \n\n for (int i =0; i<v.size(); i++)\n {\n ListNode* curr = v[i].first;\n temp->next = curr;\n temp = temp->next;\n }\n\n return head1->next;\n }\n};",
"memory": "77631"
} |
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>
| 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* sortList(ListNode* head) {\n //the brute force will be to store it in a set and then remove from the set 1 by 1\n multiset<int>s;\n while(head){\n s.insert(head->val);\n head=head->next;\n }\n ListNode*newHead=new ListNode(-1);\n ListNode*temp=newHead;\n while(!s.empty()){\n int elem=*s.begin();\n temp->next=new ListNode(elem);\n s.erase(s.find(elem));\n temp=temp->next;\n }\n return newHead->next;\n }\n};",
"memory": "78573"
} |
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>
| 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* sortList(ListNode* head) {\n multiset<int>st;\n ListNode* temp=head;\n while(temp!=NULL){\n st.insert(temp->val);\n temp=temp->next;\n }\n ListNode* dummy=new ListNode();\n temp=dummy;\n for(auto it=st.begin();it!=st.end();it++){\n ListNode* node=new ListNode(*it);\n dummy->next=node;\n dummy=node;\n } \n return temp->next; \n }\n};",
"memory": "78573"
} |
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>
| 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* sortList(ListNode* head, ListNode* follow=nullptr, double pivot=0.0) {\n if(!head){return follow;}\n ListNode* smaller=new ListNode(-1);\n ListNode* csmaller=smaller;\n ListNode* larger=new ListNode(-1);\n ListNode* clarger=larger;\n ListNode* cur = head;\n int smaller_count = 0;\n double smaller_total = 0;\n int larger_count = 0;\n double larger_total = 0;\n bool sorted=true;\n int prev=cur->val;\n while(cur){\n if(sorted && cur->val<prev){ sorted=false; prev=cur->val;}\n if(cur->val <= pivot){\n smaller_count++;\n smaller_total += cur->val;\n auto s = cur;\n cur=cur->next;\n s->next = nullptr;\n csmaller->next = s;\n csmaller = s;\n }\n else{\n larger_count++;\n larger_total += cur->val;\n auto s = cur;\n cur=cur->next;\n s->next = nullptr;\n clarger->next = s;\n clarger= s;\n }\n }\n {// delete dummy\n //auto t = smaller;\n smaller = smaller->next;\n //delete t;\n //t = larger;\n larger= larger->next;\n //delete t;\n }\n ListNode* rl = nullptr;\n if(larger_count==0){\n if(sorted){\n csmaller->next = follow;\n return smaller;\n }\n rl = follow;\n }\n else{\n rl = sortList(larger,follow,larger_total/larger_count);\n }\n auto ll = smaller_count!=0 ? sortList(smaller,rl,smaller_count!=0 ? smaller_total/smaller_count : 0) : rl;\n return ll;\n }\n};",
"memory": "79516"
} |
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>
| 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* sortList(ListNode* head) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n \n if(!head || !head->next)\n return head;\n \n vector<int> srt(200001,0);\n\n ListNode *h1 = head;\n int mn = INT_MAX, mx = INT_MIN;\n\n while(h1) {\n srt[h1->val+100000]++;\n\n mn = min(mn,h1->val);\n mx = max(mx,h1->val);\n\n h1 = h1->next;\n }\n\n h1 = head;\n mn += 100000;\n mx += 100000;\n\n for(int c1=mn;c1<=mx;c1++) {\n while(srt[c1]--) {\n h1->val = c1-100000;\n h1 = h1->next;\n }\n }\n \n return head;\n }\n};",
"memory": "79516"
} |
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>
| 3 | {
"code": "class Solution {\npublic:\n ListNode* sortList(ListNode* head) {\n\n if(head == NULL || head->next == NULL)\n return head;\n \n multiset<int> s;\n\n while(head != NULL){\n\n s.insert(head->val);\n ListNode* d = head;\n head = head->next;\n delete d;\n }\n\n ListNode* p;\n ListNode* h;\n int i = 0;\n\n for(auto& e: s){\n\n if(i == 0){\n\n h = new ListNode(e,NULL);\n i = 1;\n p = h;\n continue;\n }\n\n ListNode* n = new ListNode(e,NULL);\n p->next = n;\n p = n;\n \n }\n\n return h;\n \n }\n};",
"memory": "80458"
} |
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>
| 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* sortL(ListNode* p,int l) {\n if(l==2)\n {\n if(p->val>p->next->val)\n {\n ListNode* n=p->next;\n p->next=nullptr;\n n->next=p;\n return n;\n }\n else\n return p;\n }\n else if(l==1)\n return p;\n else if(l<1)\n return nullptr;\n \n ListNode* newleft=new ListNode;\n ListNode* newright=new ListNode;\n ListNode* pp=p;\n ListNode* lfirst=newleft;\n ListNode* rfirst=newright;\n\n for(int i=0;i<l/2;i++)\n p=p->next;\n int rnum=0;\n while(pp)\n {\n if(pp->val>p->val)\n {\n rfirst->next=pp;\n rfirst=rfirst->next;\n rnum++;\n }\n else\n {\n lfirst->next=pp;\n lfirst=lfirst->next;\n }\n pp=pp->next;\n if(pp==p)\n pp=pp->next;\n }\n \n lfirst->next=nullptr;\n rfirst->next=nullptr;\n p->next=sortL(newright->next,rnum); \n ListNode* getl=sortL(newleft->next,l-1-rnum);\n delete newleft;\n delete newright;\n if(getl)\n {\n ListNode* temp=getl;\n while(getl->next)\n getl=getl->next;\n getl->next=p;\n return temp;\n }\n else\n return p; \n }\n ListNode* sortList(ListNode* head) {\n ListNode* p=head;\n int length=0;\n while(p)\n {\n length++;\n p=p->next;\n }\n return sortL(head,length);\n\n }\n};",
"memory": "80458"
} |
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>
| 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* sortList(ListNode* head) {\n int* arr = new int[200001];\n for(int i=0; i<200001; i++) arr[i] = 0;\n ListNode* temp = head;\n while(temp != nullptr){\n arr[temp->val + 100000]++;\n temp = temp->next;\n }\n temp = head;\n for(int i=0; i<200001; i++){\n if(temp == nullptr) break;\n while(arr[i] != 0) {\n temp->val = i - 100000;\n temp = temp->next;\n arr[i]--;\n }\n }\n delete []arr;\n return head;\n }\n};",
"memory": "81401"
} |
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>
| 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 ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {\n if(list1 == NULL){\n return list2;\n }\n if(list2 == NULL){\n return list1;\n }\n\n ListNode* head = NULL;\n ListNode* temp = head;\n ListNode* temp1 = list1;\n ListNode* temp2 = list2;\n\n while(temp1!=NULL && temp2!=NULL){\n if(temp1->val < temp2->val){\n if(head == NULL){\n head = new ListNode(temp1->val);\n temp = head;\n }\n else{\n temp->next = temp1;\n temp = temp->next;\n }\n temp1 = temp1->next;\n }\n else{\n if(head == NULL){\n head = new ListNode(temp2->val);\n temp = head;\n }\n else{\n temp->next = temp2;\n temp = temp->next;\n }\n temp2 = temp2->next;\n }\n }\n\n if(temp1!=NULL){\n temp->next = temp1;\n //temp = temp->next;\n //temp1 = temp1->next;\n }\n if(temp2!=NULL){\n temp->next = temp2;\n //temp = temp->next;\n //temp2 = temp2->next;\n }\n\n return head;\n }\n ListNode* middleNode(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast = head;\n\n while(fast->next != NULL && fast->next->next!= NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n }\n ListNode* sortList(ListNode* head) {\n if(!head || !head->next){\n return head;\n }\n ListNode* temp1 = head;\n ListNode* mid = middleNode(temp1);\n ListNode* temp2 = mid->next;\n mid->next = NULL;\n\n ListNode* a = sortList(temp1);\n ListNode* b = sortList(temp2);\n return mergeTwoLists(a,b);\n\n }\n};",
"memory": "82343"
} |
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>
| 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 ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {\n if(list1 == NULL){\n return list2;\n }\n if(list2 == NULL){\n return list1;\n }\n\n ListNode* head = NULL;\n ListNode* temp = head;\n ListNode* temp1 = list1;\n ListNode* temp2 = list2;\n\n while(temp1!=NULL && temp2!=NULL){\n if(temp1->val < temp2->val){\n if(head == NULL){\n head = new ListNode(temp1->val);\n temp = head;\n }\n else{\n temp->next = temp1;\n temp = temp->next;\n }\n temp1 = temp1->next;\n }\n else{\n if(head == NULL){\n head = new ListNode(temp2->val);\n temp = head;\n }\n else{\n temp->next = temp2;\n temp = temp->next;\n }\n temp2 = temp2->next;\n }\n }\n\n if(temp1!=NULL){\n temp->next = temp1;\n //temp = temp->next;\n //temp1 = temp1->next;\n }\n if(temp2!=NULL){\n temp->next = temp2;\n //temp = temp->next;\n //temp2 = temp2->next;\n }\n\n return head;\n }\n ListNode* middleNode(ListNode* head) {\n ListNode* slow = head;\n ListNode* fast = head;\n\n while(fast->next != NULL && fast->next->next!= NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n }\n ListNode* sortList(ListNode* head) {\n if(!head || !head->next){\n return head;\n }\n ListNode* temp1 = head;\n ListNode* mid = middleNode(temp1);\n ListNode* temp2 = mid->next;\n mid->next = NULL;\n\n temp1 = sortList(temp1);\n temp2 = sortList(temp2);\n return mergeTwoLists(temp1,temp2);\n\n }\n};",
"memory": "83286"
} |
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>
| 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* sortList(ListNode* head) {\n if (!head) return nullptr;\n map<int,vector<ListNode*>> m;\n ListNode* tail = head;\n while (tail) {\n if (tail==nullptr) break;\n m[tail->val].push_back(tail);\n tail = tail->next;\n }\n ListNode* newHead = m.begin()->second.back();\n m.begin()->second.pop_back();\n ListNode* prev = newHead;\n for (auto it = m.begin(); it!=m.end();it++) {\n while (it->second.size()) {\n if (prev) {\n prev->next = it->second.back();\n }\n prev = it->second.back();\n it->second.pop_back();\n }\n }\n prev->next = nullptr;\n return newHead;\n }\n};",
"memory": "83286"
} |
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>
| 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 {\n ListNode* findMid(ListNode* head){\n if(head==NULL){\n return NULL;\n }\n ListNode* slow = head;\n ListNode* fast = head->next;\n while(fast != NULL && fast->next != NULL){\n slow = slow->next;\n fast = fast->next->next;\n }\n return slow;\n }\n ListNode* merge(ListNode* left, ListNode* right){\n if(left == NULL) return right;\n if(right == NULL) return left;\n ListNode* ans = new ListNode(-1);\n ListNode* temp = ans;\n while(left != NULL && right != NULL){\n if(left->val < right->val){\n temp->next = left;\n temp = left;\n left = left->next;\n }\n else{\n temp->next = right;\n temp = right;\n right = right->next;\n }\n }\n while(left != NULL){\n temp->next = left;\n temp = left;\n left = left->next;\n }\n while(right != NULL){\n temp->next = right;\n temp = right;\n right = right->next;\n }\n\n \n ListNode* result = ans->next;\n delete ans;\n return result;\n }\npublic:\n ListNode* sortList(ListNode* head) {\n if(head==NULL || head->next == NULL){\n return head;\n }\n ListNode* mid = findMid(head);\n ListNode* left = head;\n ListNode* right = mid->next;\n mid->next = NULL;\n left = sortList(left);\n right = sortList(right);\n\n ListNode* result = merge(left,right);\n return result;\n }\n};",
"memory": "84228"
} |
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>
| 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 {\n ListNode* merge(ListNode* head1,ListNode* head2){\n ListNode* dummy=new ListNode(-1);\n ListNode* temp=dummy;\n ListNode *temp1=head1;\n ListNode *temp2=head2;\n while(temp1 && temp2){\n if(temp1->val<=temp2->val){\n temp->next=temp1;\n temp=temp1;\n temp1=temp1->next;\n\n }\n else{\n temp->next=temp2;\n temp=temp2;\n temp2=temp2->next;\n }\n }\n if(temp1){\n temp->next=temp1;\n }\n else if(temp2){\n temp->next=temp2;\n }\n ListNode* newHead=dummy->next;\n delete dummy;\n return newHead;\n\n }\n ListNode* findMiddle(ListNode* head){\n ListNode* slow=head;\n ListNode* fast=head;\n while(fast->next && fast->next->next){\n slow=slow->next;\n fast=fast->next->next;\n }\n return slow;\n }\npublic:\n ListNode* sortList(ListNode* head) {\n if(head==NULL || head->next==NULL) return head;\n ListNode* middle=findMiddle(head);\n ListNode* head1=head;\n ListNode* head2=middle->next;\n middle->next=NULL;\n ListNode* newHead1=sortList( head1);\n ListNode* newHead2=sortList( head2);\n return merge(newHead1,newHead2);\n }\n};",
"memory": "84228"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n if(n <= 2) return n;\n int count = 2;\n\n for(int i = 0; i < n; i++){\n for(int j = i + 1; j < n; j++){\n int tp = 2;\n for(int k = 0; k < n; k++){\n int x1 = points[i][0];\n int y1 = points[i][1];\n\n int x2 = points[j][0];\n int y2 = points[j][1];\n\n int x3 = points[k][0];\n int y3 = points[k][1];\n\n if(((y2 - y1) * (x3 - x1)) == ((y3 - y1) * (x2 - x1)) && (k!=i) && (k!=j)){\n tp++;\n }\n }\n count = max(tp, count);\n }\n }\n return count;\n }\n};",
"memory": "9571"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class LinearFunction{\nprivate:\n int b;\n int yConstant;\n int a;\n int xConstant;\n \npublic:\n LinearFunction(vector<int>& pointA, vector<int>& pointB){\n b = pointA[1];\n yConstant = pointA[0] - pointB[0];\n a = pointA[0];\n xConstant = pointA[1] - pointB[1];\n }\n\n bool isOnLine(vector<int>& point) {\n int value = (point[1] - b) * yConstant - (point[0] - a) * xConstant;\n return value == 0;\n }\n};\n\nint numPointsOnLine(LinearFunction& l, vector<vector<int>>& points) {\n int result = 0;\n for (auto& point: points) {\n if (l.isOnLine(point))\n result++;\n }\n return result;\n}\n\n\nclass Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n if (points.size() <= 2)\n return points.size();\n\n int maxValue = 0;\n for (int i=0; i<int(points.size()); i++) {\n for (int j=i+1; j<int(points.size()); j++) {\n LinearFunction l(points[j], points[i]);\n maxValue = max(maxValue, numPointsOnLine(l, points));\n } \n }\n\n return maxValue;\n }\n};",
"memory": "9571"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n // define dp[i][j] = number of points on the line\n // passing through points[i] and points[j]\n vector<vector<int>> dp(n, vector<int>(n));\n int x_diff, y_diff, left, right;\n int ans = 1;\n for (int i = 0; i < n; i++) {\n for (int j = i+1; j < n; j++) {\n dp[i][j] = 0;\n x_diff = points[j][0] - points[i][0];\n y_diff = points[j][1] - points[i][1];\n for (int k = 0; k < n; k++) {\n left = y_diff * (points[k][0] - points[i][0]);\n right = (points[k][1] - points[i][1]) * x_diff;\n dp[i][j] += (left == right);\n }\n ans = max(ans, dp[i][j]);\n }\n }\n return ans;\n }\n};",
"memory": "10713"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int lis(vector<vector<int>>& points, const pair<int,int>& prev1, const pair<int,int>& prev2, int i) {\n if (i >= points.size()) return 0;\n\n int take = 0; \n\n if ((prev1.first == 1e9 && prev1.second == 1e9) || \n (prev2.first == 1e9 && prev2.second == 1e9) || \n ((prev1.first - prev2.first) * (prev1.second - points[i][1]) == \n (prev1.first - points[i][0]) * (prev1.second - prev2.second))) {\n\n \n take = 1 + lis(points, {points[i][0], points[i][1]}, prev1, i + 1);\n }\n\n int ntake = lis(points, prev1, prev2, i + 1);\n return max(take, ntake);\n }\n\npublic:\n int maxPoints(vector<vector<int>>& points) {\n return lis(points, {1e9, 1e9}, {1e9, 1e9}, 0);\n }\n};\n",
"memory": "10713"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n using Point = vector<int>;\n const int bitOffset = 32768;\n struct Direction \n {\n int16_t X;\n int16_t Y;\n int Count = 2; // Defaults to 2 because a line must have a minimum of 2 points to exist.\n };\n\n int maxPoints(vector<vector<int>>& points) {\n int numPoints = points.size();\n if (points.size() < 2) return numPoints;\n\n std::map<int, int> counts;\n int maxCount = 2;\n\n for (int i = 0; i < numPoints-1; i++)\n {\n vector<Direction> dirMap;\n Point& p1 = points[i];\n\n for (int j = i+1; j<numPoints; j++)\n {\n Point& p2 = points[j];\n Direction d;\n d.X = p2[0] - p1[0];\n d.Y = p2[1] - p1[1];\n \n int count = 2; // Defaults to 2 because a line must have a minimum of 2 points to exist.\n for (Direction& existing : dirMap)\n {\n int cross = (d.X*existing.Y) - (d.Y*existing.X);\n \n if (cross == 0)\n {\n // We have a match!\n count = ++existing.Count;\n if (count > maxCount) maxCount = count;\n break;\n }\n }\n\n if (count == 2)\n {\n dirMap.push_back(d);\n }\n }\n\n if (numPoints - i - 1 <= maxCount) break; // Avoid final iterations when we can't possibly find a larger set\n }\n\n return maxCount;\n }\n};",
"memory": "11856"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n using Point = vector<int>;\n const int bitOffset = 32768;\n struct Direction \n {\n int16_t X;\n int16_t Y;\n int Count = 2; // Defaults to 2 because a line must have a minimum of 2 points to exist.\n };\n\n int maxPoints(vector<vector<int>>& points) {\n const int numPoints = points.size();\n if (points.size() < 2) return numPoints;\n\n int maxCount = 2;\n\n for (int i = 0; i < numPoints-1; i++)\n {\n vector<Direction> dirMap;\n const Point& p1 = points[i];\n\n for (int j = i+1; j<numPoints; j++)\n {\n const Point& p2 = points[j];\n Direction d;\n d.X = p2[0] - p1[0];\n d.Y = p2[1] - p1[1];\n \n int count = 2; // Defaults to 2 because a line must have a minimum of 2 points to exist.\n for (Direction& existing : dirMap)\n {\n const int cross = (d.X*existing.Y) - (d.Y*existing.X);\n \n if (cross == 0)\n {\n // We have a match!\n count = ++existing.Count;\n if (count > maxCount) maxCount = count;\n break;\n }\n }\n\n if (count == 2)\n {\n dirMap.push_back(d);\n }\n }\n\n if (numPoints - i - 1 <= maxCount) break; // Avoid final iterations when we can't possibly find a larger set\n }\n\n return maxCount;\n }\n};",
"memory": "11856"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n vector<vector<bool>> pairs(n, vector<bool>(n, false));\n int res = 1;\n for (int i = 0; i < n; i++) {\n for (int j = i+1; j < n; j++) {\n if (!pairs[i][j]) {\n int temp = 2;\n pairs[i][j] = true;\n vector<int> nodes = {i, j};\n if (points[i][0] == points[j][0]) {\n for (int k = 0; k < n; k++) {\n if (k == i || k == j) continue;\n if (points[k][0] == points[i][0]) {\n for (int l : nodes) {\n pairs[l][k] = true;\n pairs[k][l] = true;\n }\n nodes.push_back(k);\n temp++;\n }\n }\n }\n else {\n int x = points[i][0], y = points[i][1];\n int diffX = x - points[j][0];\n int diffY = y - points[j][1];\n\n for (int k = 0; k < n; k++) {\n if (k == i || k == j) continue;\n int candX = x - points[k][0];\n int candY = y - points[k][1];\n if (diffX * candY == diffY * candX) {\n for (int l : nodes) {\n pairs[l][k] = true;\n pairs[k][l] = true;\n }\n nodes.push_back(k);\n temp++;\n }\n }\n }\n \n res = max(res, temp);\n }\n }\n }\n return res;\n }\n};",
"memory": "12998"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int i,j,k,max = 1,ct;\n vector<pair<double,double>> lines;\n for(i = 0; i < points.size(); i++){\n ct = 1;\n for(j = i + 1; j < points.size(); j++){\n if(points[j][0] == points[i][0]){\n ct++;\n }\n else{\n double m = (1.0*(points[i][1]-points[j][1]))/(1.0*(points[i][0] - points[j][0]));\n double c = points[i][1] - m*points[i][0];\n lines.push_back(make_pair(m,c));\n }\n }\n if(ct > max){\n max = ct;\n }\n }\n sort(lines.begin(),lines.end());\n for(i = 0; i < lines.size(); i++){\n ct = 1;\n while(i < lines.size() - 1 && (lines[i+1].first - lines[i].first) < 0.000001 && (lines[i+1].second - lines[i].second) < 0.000001){\n i++;\n ct++;\n }\n if(ct > max*(max-1)/2){\n for(j = max; j <= points.size(); j++){\n if(ct == j*(j-1)/2){\n max = j;\n break;\n }\n }\n }\n }\n return max;\n }\n};",
"memory": "12998"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "const double EPS = 1e-9;\nclass Solution {\npublic:\n struct pt {\n double x, y;\n\n pt(int x, int y) {\n this->x = x;\n this->y = y;\n }\n\n bool operator<(const pt& p) const {\n return x < p.x - EPS || (abs(x - p.x) < EPS && y < p.y - EPS);\n }\n };\n\n struct l {\n double a, b, c;\n\n l(pt p, pt q) {\n a = p.y - q.y;\n b = q.x - p.x;\n c = -a * p.x - b * p.y;\n norm();\n }\n\n void norm() {\n double z = sqrt(a * a + b * b);\n if (abs(z) > EPS)\n a /= z, b /= z, c /= z;\n }\n\n double dist(pt p) const { return a * p.x + b * p.y + c; }\n };\n\n int maxPoints(vector<vector<int>>& points) {\n\n if (points.size() == 1)\n return 1;\n\n vector<l> lines;\n\n for (int i = 0; i < points.size(); i++) {\n for (int j = i + 1; j < points.size(); j++) {\n l cur = l(pt(points[i][0], points[i][1]),\n pt(points[j][0], points[j][1]));\n lines.push_back(cur);\n }\n }\n\n int ans = 0;\n\n for (auto i : lines) {\n int count = 0;\n for (int j = 0; j < points.size(); j++) {\n double cur = i.dist(pt(points[j][0], points[j][1]));\n cur = abs(cur - 0.0);\n if (cur < EPS)\n count++;\n }\n\n ans = max(ans, count);\n }\n\n return ans;\n }\n};",
"memory": "14141"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n unordered_map<float,int> mp;\n int count = 0;\n\n\n\n for(int i=0;i<points.size();i++){\n for(int j=i+1;j<points.size();j++){\n // if(i == j) continue;\n float slope = (float)(points[j][1]-points[i][1])/(points[j][0]-points[i][0]);\n if (abs(slope) > INT_MAX) slope = abs(slope);\n mp[slope]++;\n }\n int maxSlope = 0;\n for(auto x:mp) maxSlope = max(maxSlope,x.second);\n mp.clear();\n count = max(count,maxSlope+1);\n }\n\n return count;\n }\n};",
"memory": "14141"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "#include <vector>\n#include <unordered_map>\n#include <memory>\n#include <cmath>\n\n//nije dobro ak je a==b (0) ili ak se dogodi slucaj hash(a,b) i hash(b,a) jer su isti\nclass XorPairHash {\npublic:\n template <class _T1, class _T2>\n std::size_t operator()(const std::pair<_T1, _T2>& p) const {\n return std::hash<float>()(p.first) ^ std::hash<float>()(p.second);\n }\n};\n\nclass Solution {\nprivate:\n //ax+b, bucketing\n std::unordered_map<std::pair<float, float>, int, XorPairHash> aib;\n\npublic:\n int maxPoints(std::vector<std::vector<int>>& points) {\n int max = 0;\n aib.reserve(points.size() * (points.size() - 1) / 2);\n for (std::size_t i = 0; i < points.size(); i++) {\n for (std::size_t j = i + 1; j < points.size(); j++) {\n auto ref = ++aib[calcAiB(points[i], points[j])];\n if (ref > max) {\n max = ref;\n }\n }\n }\n\n //this is because we overcount, max is real choose 2 so this is solving for real, x^2 - x - 2 max = 0 and we know x is a natural number\n return (1 + static_cast<int>(std::sqrt(8 * max + 1))) / 2;\n }\n\n std::pair<float, float> calcAiB(const std::vector<int>& point1, const std::vector<int>& point2) const {\n float a = (static_cast<float>(point1[1]) - static_cast<float>(point2[1])) / (static_cast<float>(point1[0]) - static_cast<float>(point2[0]));\n [[unlikely]] if (std::isinf(a)) {\n return std::make_pair(static_cast<float>(1) / static_cast<float>(0), point1[0]);\n }\n auto temp = std::make_pair(a, round((static_cast<float>(point1[1]) - a * static_cast<float>(point1[0])) * 1000.0f));\n return temp;\n }\n};",
"memory": "15283"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "struct Vector {\n int x;\n int y;\n\n Vector operator - (const Vector& b) const {\n return Vector {\n x - b.x,\n y - b.y,\n };\n }\n\n bool operator != (const Vector& b) const {\n return x != b.x || y != b.y;\n }\n};\n\nbool lex_less_than(Vector a, Vector b) {\n if(a.x != b.x) {\n return a.x < b.x;\n } else {\n return a.y < b.y;\n }\n}\n\nint64_t cross(Vector a, Vector b) {\n return static_cast<int64_t>(a.x) * static_cast<int64_t>(b.y) -\n static_cast<int64_t>(a.y) * static_cast<int64_t>(b.x);\n}\n\nstruct Line {\n Vector point;\n Vector dir;\n};\n\nbool cmp(Line a, Line b) {\n int64_t c = cross(a.dir, b.dir);\n if(c != 0) {\n return c > 0;\n }\n\n c = cross(a.dir, b.point - a.point);\n if(c != 0) {\n return c > 0;\n }\n\n return lex_less_than(a.point, b.point);\n}\n\nclass Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n if(points.size() <= 2) {\n return static_cast<int>(points.size());\n }\n\n std::vector<Line> lines;\n for(int i = 0; i < points.size(); i++) {\n Vector a{points[i][0], points[i][1]};\n for(int j = i + 1; j < points.size(); j++) {\n Vector b{points[j][0], points[j][1]};\n if(lex_less_than(a, b)) {\n lines.push_back(Line{a, b - a});\n } else {\n lines.push_back(Line{b, a - b});\n }\n }\n }\n\n std::sort(lines.begin(), lines.end(), cmp);\n\n for(auto line : lines) {\n std::cout << \"[\" << line.point.x << \", \" << line.point.y << \"], [\" <<\n line.dir.x << \", \" << line.dir.y << \"]\" << std::endl;\n }\n\n int result = 0;\n \n Line block_first_line = lines[0];\n int block_result = 2;\n\n for(int i = 1; i < lines.size(); i++) {\n if(block_first_line.point != lines[i].point ||\n cross(block_first_line.dir, lines[i].dir) != 0\n ) {\n result = std::max(result, block_result);\n block_result = 2;\n block_first_line = lines[i];\n } else {\n block_result++;\n }\n }\n\n result = std::max(result, block_result);\n\n return result;\n }\n};",
"memory": "15283"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "struct pairHash {\n int operator()(const std::pair<int, int> &a) const {\n return a.first ^ a.second;\n }\n};\n\nclass Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n if (n <= 2) {\n return n;\n }\n int res = 0;\n for (int i = 0; i < n; ++i) {\n std::unordered_map<std::pair<int, int>, int, pairHash> map;\n for (int j = 0; j < n; ++j) {\n if (j == i) continue;\n int dx = points[i][0] - points[j][0];\n int dy = points[i][1] - points[j][1];\n int g = gcd(dx,dy);\n if (g != 0) {\n dx /= g;\n dy /= g;\n }\n map[{dx, dy}]++;\n }\n for (auto [s, count] : map) {\n res = std::max(res, count);\n }\n }\n return res + 1;\n }\n};",
"memory": "23281"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n if (n < 2) return n;\n int max_points = 1;\n for(int i = 0; i < n-1; i++){\n unordered_map<double, int> slope_freq;\n int verticals = 0;\n int duplicates = 0;\n for(int j = i + 1; j < n; j++){\n auto p1 = points[i];\n auto p2 = points[j];\n\n if (p1 == p2) duplicates++;\n else if(p1[0] == p2[0]) verticals++;\n else{\n double slope = double(p2[1] - p1[1])/double(p2[0] - p1[0]);\n slope_freq[slope]++;\n }\n\n }\n int temp = verticals;\n for(auto [slope, freq] : slope_freq) temp = max(temp, freq);\n max_points = max(max_points, temp + duplicates + 1);\n }\n\n return max_points;\n }\n};",
"memory": "23281"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int result=0;\n int n=points.size(); \n if(n==1) return 1;\n\n for(int i=0;i<n;i++){\n unordered_map<double,int>mp;\n\n for(int j=0;j<n;j++){\n if(i==j) continue;\n int dy=points[j][1]-points[i][1];\n int dx=points[j][0]-points[i][0];\n auto slope=atan2(dy,dx);\n mp[slope]++;\n }\n for(auto &it:mp){\n result=max(result,it.second+1);\n }\n }\n \n\n return result;\n }\n};",
"memory": "24423"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n auto size = points.size();\n if (size == 1) return 1;\n int ans = 2;\n for (int i = 0; i < size; i++) {\n unordered_map<double ,int> m{};\n for (int j = 0; j < size; j++) {\n if (i != j) {\n m[atan2(points[j][1]-points[i][1], points[j][0]-points[i][0])]++;\n }\n }\n for (auto [h, count]: m) {\n ans = max(ans, count+1);\n }\n }\n\n return ans;\n }\n};",
"memory": "24423"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n\npublic:\n// Function to calculate the greatest common divisor (GCD)\nint gcd(int a, int b) {\n if (b == 0) return a;\n return gcd(b, a % b);\n}\n\n// Function to create a string representation of a line using slope and intercept\nstring getLineKey(pair<int, int> p1, pair<int, int> p2) {\n int dx = p2.first - p1.first;\n int dy = p2.second - p1.second;\n\n if (dx == 0) { // Vertical line, slope is undefined\n return \"inf_\" + to_string(p1.first); // x = constant (vertical line)\n }\n\n int g = gcd(dx, dy); // Simplify the slope\n dx /= g;\n dy /= g;\n\n // Normalize slope direction\n if (dx < 0) {\n dx = -dx;\n dy = -dy;\n }\n\n // Calculate intercept using y = mx + b -> b = y - m * x\n int intercept_numerator = p1.second * dx - p1.first * dy;\n int intercept_denominator = dx;\n\n // Simplify the intercept\n int g_intercept = gcd(intercept_numerator, intercept_denominator);\n intercept_numerator /= g_intercept;\n intercept_denominator /= g_intercept;\n\n return to_string(dy) + \"/\" + to_string(dx) + \"_\" + to_string(intercept_numerator) + \"/\" + to_string(intercept_denominator);\n}\n\n// Function to find the maximum number of points that lie on the same line\nint maxPoints(vector<vector<int>>& points) {\n if (points.size() <= 2) return points.size();\n\n int max_points = 1;\n\n for (int i = 0; i < points.size(); i++) {\n unordered_map<string, int> line_count;\n int duplicates = 1;\n int cur_max = 0;\n\n for (int j = i + 1; j < points.size(); j++) {\n if (points[i] == points[j]) {\n duplicates++; // Count duplicates\n } else {\n string line_key = getLineKey({points[i][0], points[i][1]}, {points[j][0], points[j][1]});\n line_count[line_key]++;\n cur_max = max(cur_max, line_count[line_key]);\n }\n }\n\n max_points = max(max_points, cur_max + duplicates);\n }\n\n return max_points;\n}\n\n};",
"memory": "25566"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n vector<double> v(n);\n vector<vector<double>> slopes(n, v);\n\n for(int i=0;i<n;i++) {\n for(int j=0;j<n;j++) {\n if(points[i][0]==points[j][0])\n slopes[i][j] = INT_MAX;\n else\n slopes[i][j] = double(points[j][1] - points[i][1])/(points[j][0] - points[i][0]);\n }\n }\n int ans = 1;\n for(int i=0;i<n;i++) {\n unordered_map<double, int> m;\n for(int j=0;j<n;j++) {\n \n if(i!=j){\n m[slopes[i][j]]++;\n ans = max(ans, m[slopes[i][j]]);\n }\n \n }\n\n }\n\n\n // for(int i=0;i<n;i++) {\n // for(int j=0;j<n;j++) {\n // cout<<slopes[i][j]<<\" \";\n // }\n // cout<<\"\\n\";\n // }\n if(ans==n)\n return n;\n\n \n return ans+1;\n }\n};",
"memory": "25566"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int max = 0;\n if(points.size() == 1) return 1;\n\n for(int i = 0; i < points.size()-1; i++)\n {\n vector<int> point_1 = points[i];\n unordered_map<double, int> record;\n for(int j = i+1; j < points.size(); j++)\n {\n vector<int> point_2 = points[j];\n double slope = caculate_slope(point_1, point_2);\n printf(\"slope:%lf\\n\", slope);\n printf(\"\\n\");\n record[slope]+=1;\n max = record[slope] > max? record[slope]: max;\n }\n }\n max++;\n return max;\n\n \n }\n\n double caculate_slope(vector<int> point_a, vector<int> point_b)\n {\n printf(\"point_1:%d\\n\", point_a[0]);\n printf(\"point_2:%d\\n\", point_b[0]);\n if(point_a[0] == point_b[0]) return INT_MIN;\n double dvided = (point_a[0] - point_b[0]);\n return (point_a[1] - point_b[1]) / dvided;\n }\n};",
"memory": "26708"
} |
149 | <p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
<strong>Output:</strong> 3
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg" style="width: 300px; height: 294px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
<strong>Output:</strong> 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= points.length <= 300</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code></li>
<li>All the <code>points</code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int maxPoints(vector<vector<int>>& points) {\n int max = 0;\n if(points.size() == 1) return 1;\n\n for(int i = 0; i < points.size()-1; i++)\n {\n vector<int> point_1 = points[i];\n unordered_map<double, int> record;\n for(int j = i+1; j < points.size(); j++)\n {\n vector<int> point_2 = points[j];\n double slope = caculate_slope(point_1, point_2);\n record[slope]+=1;\n max = record[slope] > max? record[slope]: max;\n }\n }\n max++;\n return max;\n\n \n }\n\n double caculate_slope(vector<int> point_a, vector<int> point_b)\n {\n printf(\"point_1:%d\\n\", point_a[0]);\n printf(\"point_2:%d\\n\", point_b[0]);\n if(point_a[0] == point_b[0]) return INT_MIN;\n double dvided = (point_a[0] - point_b[0]);\n return (point_a[1] - point_b[1]) / dvided;\n }\n};",
"memory": "26708"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.