Comments (5)
Yes, the recursive Merge Sort requires O(N) auxiliary space.
If we use non-recursive way, the code might be hard to read.
Anyway, do you have any good solution we can have readable code and good performance both?
from leetcode.
😄 It's simple. Here is my code:
class Solution {
public:
// The bottom-up merge-sort. (non-recursive)
ListNode* sortList(ListNode* head) {
ListNode fakeHead(0);
fakeHead.next = head;
int length = 1;
ListNode* subLeft = fakeHead.next;
ListNode* subRight = cut(subLeft, length);
while (subRight != NULL) {
ListNode* tail = &fakeHead;
do { // merge every two adjacent lists
ListNode* nextSubLeft = cut(subRight, length);
tail = merge(subLeft, subRight, tail);
subLeft = nextSubLeft;
subRight = cut(subLeft, length);
} while (subLeft != NULL);
length *= 2;
subLeft = fakeHead.next;
subRight = cut(subLeft, length);
}
return fakeHead.next;
}
private:
ListNode* cut(ListNode* head, int length) {
while (head != NULL && --length > 0) head = head->next;
if (head == NULL) return NULL;
ListNode* next = head->next;
head->next = NULL;
return next;
}
ListNode* merge(ListNode* l1, ListNode* l2, ListNode* tail) {
while (l1 != NULL && l2 != NULL) {
if (l1->val < l2->val) {
tail->next = l1;
l1 = l1->next;
} else {
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
tail->next = (l1 != NULL ? l1 : l2);
while (tail->next != NULL) tail = tail->next;
return tail;
}
};
It can be further optimized if you give it less readability.
from leetcode.
@jakwings Wow, you are really good at code, I am too old to read such long code. ;-)
from leetcode.
Hey, there are only two new blocks, not that long as it seems. The merge
function is trivial, also used in some other problems.
from leetcode.
@haoel Have a look at this.
class Solution
{
public:
ListNode *merge(ListNode *l1, ListNode *l2)
{
if (!l1)
return l2;
if (!l2)
return l1;
if (l1->val < l2->val)
{
l1->next = merge(l1->next, l2);
return l1;
}
else
{
l2->next = merge(l1, l2->next);
return l2;
}
}
ListNode *sortList(ListNode *head)
{
if (head == nullptr or head->next == nullptr)
return head;
//// step 1. cut the list to two halves
ListNode *slow = head;
ListNode *fast = head->next; // yrr isse na second half ka mid consider nhi ho rha jab list even hain uss case meinn
while (fast and fast->next)
{
slow = slow->next;
fast = fast->next->next;
}
ListNode *head2 = slow->next;
slow->next = nullptr;
// step 2. divide each parth into further halves
ListNode *l1 = sortList(head);
ListNode *l2 = sortList(head2);
// step 3. merge l1 and l2
return merge(l1, l2);
}
};
from leetcode.
Related Issues (20)
- Array out of bounds in ”Divide Two Integers“ ‘s solution.
- ReadMe Description error
- 09.palindrome-number HOT 1
- reverse-integer more easy understand
- please include a contributing.md in your repo HOT 1
- question1 twoSum
- 160. intersectionOfTwoLinkedLists.cpp HOT 2
- What is the license for this repository? HOT 1
- Problem 400: Nth Digit - is Medium difficulty on LeetCode not Easy difficulty
- The xidel installed by query_problem.sh is not support for MacOs Catalina 10.15.3 HOT 1
- All examples as absent
- A problem about 315. Count of Smaller Numbers After Self HOT 1
- out of range HOT 1
- Add a daily challenge section
- The install_xidel function for mac in query_problem.sh is invaliad HOT 1
- 1: Two Sum: Go: signature is too broad
- 3sum smaller is missing
- TenthLine - bash
- OutOfMemory exception HOT 3
- Runtime Error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode.