This one is easy. Merge sort can solve this problem.
See the following code for the merge sort from bottom to top
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *loop_list(ListNode *head, int len){
// There are len nodes from head(inclusive) to ret(exclusive)
while(head != NULL && len-- > 0) head = head -> next;
return head;
}
ListNode *merge(ListNode *head, int len){
// Merge len nodes beginning from head->next(inclusive), and return the last node.
if(head == NULL || head -> next == NULL) return NULL;
ListNode *a_beg = head -> next;
ListNode *a_end = loop_list(a_beg, len/2 - 1);
if(!a_end) return NULL;
ListNode *b_beg = a_end -> next;
a_end -> next = NULL;
ListNode *b_end = loop_list(b_beg, len/2 - 1);
ListNode *new_end = b_end == NULL ? NULL : b_end -> next;
if(b_end != NULL) b_end -> next = NULL;
ListNode *loop = head;
ListNode *a_loop = a_beg;
ListNode *b_loop = b_beg;
while(a_loop || b_loop){
ListNode *next;
if(!a_loop) {
next = b_loop;
}
else if(!b_loop){
next = a_loop;
}
else next = (a_loop -> val < b_loop -> val) ? a_loop : b_loop;
loop -> next = next;
loop = loop -> next;
if(next == a_loop){
a_loop = a_loop -> next;
}
else if(next == b_loop){
b_loop = b_loop -> next;
}
}
loop -> next = new_end;
return loop;
}
ListNode *sortList(ListNode *head) {
ListNode *fake_head = new ListNode(0);
fake_head -> next = head;
int num_merge = 0;
for(int len = 2; true; len *= 2){
num_merge = 0;
ListNode *loop = fake_head;
while(loop && loop->next){
loop = merge(loop, len);
num_merge ++;
}
if(num_merge <= 1) break;
}
head = fake_head -> next;
delete fake_head;
return head;
}
};
Subscribe to:
Post Comments (Atom)
Manacher's Longest Palindromic Substring Algorithm
http://manacher-viz.s3-website-us-east-1.amazonaws.com/#/
-
Suppose a bus is running on a loop route. It takes 10 mins for the bus to finish the route. If a guy arrives at a bus stop at uniformly rand...
-
Question: You are playing a card game with me. Suppose I shuffle a deck of 52 cards, then I show you the card one by one to you from top t...
-
Move Objects There are N objects kept in a row. The ith object is at position x_i. You want to partition them into K groups. You want ...
No comments:
Post a Comment