原题题目
代码实现(首刷自解)
class Solution {
public :
static bool cmp ( const ListNode* a, const ListNode* b)
{
return a-> val < b-> val;
}
ListNode* sortList ( ListNode* head) {
if ( ! head) return nullptr ;
vector< ListNode* > unsorted_vector;
auto temp = head;
while ( temp)
{
unsorted_vector. push_back ( temp) ;
temp = temp-> next;
}
stable_sort ( unsorted_vector. begin ( ) , unsorted_vector. end ( ) , cmp) ;
unsorted_vector. push_back ( nullptr ) ;
for ( int i= 0 ; i< unsorted_vector. size ( ) - 1 ; ++ i)
unsorted_vector[ i] -> next = unsorted_vector[ i+ 1 ] ;
return unsorted_vector[ 0 ] ;
}
} ;
代码实现(二刷懒惰写法 DAY 147 日后一定补)
class Solution {
public :
ListNode* sortList ( ListNode* head) {
if ( ! head) return head;
vector< ListNode* > v;
ListNode* ptr = head;
while ( ptr)
{
v. emplace_back ( ptr) ;
ptr = ptr-> next;
}
sort ( v. begin ( ) , v. end ( ) , [ ] ( auto & a, auto & b) { return a-> val < b-> val; } ) ;
for ( int i= 0 ; i< v. size ( ) ; ++ i)
{
if ( i!= v. size ( ) - 1 ) v[ i] -> next = v[ i+ 1 ] ;
else v[ i] -> next = nullptr ;
}
return v[ 0 ] ;
}
} ;
代码实现(三刷自解 归并链表)
class Solution {
public :
ListNode* merge ( ListNode* left, ListNode* right)
{
if ( ! left) return right;
if ( ! right) return left;
ListNode* ptr, * tmp;
if ( left-> val <= right-> val)
{
ptr = left;
left = left-> next;
}
else
{
ptr = right;
right = right-> next;
}
tmp = ptr;
while ( left && right)
{
if ( left-> val <= right-> val)
{
tmp-> next = left;
left = left-> next;
}
else
{
tmp-> next = right;
right = right-> next;
}
tmp = tmp-> next;
}
if ( ! right) tmp-> next = left;
if ( ! left) tmp-> next = right;
return ptr;
}
ListNode* merge_list ( ListNode* head)
{
if ( ! head || ! head-> next) return head;
ListNode* fast = head, * slow = head, * breakptr = head;
while ( fast && fast-> next)
{
fast = fast-> next;
if ( ! fast-> next) break ;
fast = fast-> next;
breakptr = breakptr-> next;
}
fast = breakptr-> next;
breakptr-> next = nullptr ;
ListNode* left = merge_list ( slow) ;
ListNode* right = merge_list ( fast) ;
return merge ( left, right) ;
}
ListNode* sortList ( ListNode* head) {
return merge_list ( head) ;
}
} ;
代码实现(四刷自解 归并算法 C++ DAY 218)
class Solution {
public :
ListNode* merge_two_list ( ListNode* root1, ListNode* root2)
{
if ( ! root1) return root2;
if ( ! root2) return root1;
ListNode* ret = nullptr , * tmp = nullptr ;
while ( root1 && root2)
{
if ( root1-> val <= root2-> val)
{
if ( ! tmp)
{
ret = root1;
tmp = ret;
}
else
{
tmp-> next = root1;
tmp = tmp-> next;
}
root1 = root1-> next;
}
else
{
if ( ! tmp)
{
ret = root2;
tmp = ret;
}
else
{
tmp-> next = root2;
tmp = tmp-> next;
}
root2 = root2-> next;
}
}
if ( root1) tmp-> next = root1;
if ( root2) tmp-> next = root2;
return ret;
}
ListNode* helper ( ListNode* head)
{
if ( ! head || ! head-> next) return head;
ListNode* slow = head, * fast = head-> next;
while ( fast && fast-> next)
{
slow = slow-> next;
fast = fast-> next-> next;
}
ListNode* ret = slow-> next;
slow-> next = nullptr ;
return ret;
}
ListNode* merge_sort ( ListNode* head)
{
if ( ! head || ! head-> next) return head;
ListNode* left = head, * right = helper ( head) ;
left = merge_sort ( left) ;
right = merge_sort ( right) ;
ListNode* ret = merge_two_list ( left, right) ;
return ret;
}
ListNode* sortList ( ListNode* head) {
if ( ! head || ! head-> next) return head;
ListNode* ret = merge_sort ( head) ;
return ret;
}
} ;
代码实现(五刷自解 DAY 279 C++)
class Solution {
public :
ListNode* Merge ( ListNode* left, ListNode* right) {
if ( ! left) return right;
if ( ! right) return left;
ListNode* ret = nullptr ;
ListNode* tmp = nullptr ;
if ( left-> val < right-> val) {
ret = left;
left = left-> next;
} else {
ret = right;
right = right-> next;
}
tmp = ret;
while ( left && right) {
if ( left-> val < right-> val) {
tmp-> next = left;
left = left-> next;
} else {
tmp-> next = right;
right = right-> next;
}
tmp = tmp-> next;
}
if ( left) tmp-> next = left;
else tmp-> next = right;
return ret;
}
ListNode* MergeSort ( ListNode* head) {
if ( ! head || ! head-> next) return head;
ListNode* fast = head-> next-> next;
ListNode* slow = head;
while ( fast && fast-> next) {
fast = fast-> next-> next;
slow = slow-> next;
}
ListNode* next = slow-> next;
slow-> next = nullptr ;
head = MergeSort ( head) ;
next = MergeSort ( next) ;
return Merge ( head, next) ;
}
ListNode* sortList ( ListNode* head) {
return MergeSort ( head) ;
}
} ;
代码实现(五刷自解 快速排序 DAY 279 C++)
class Solution {
public :
ListNode* sortList ( ListNode* head) {
if ( ! head || ! head-> next) return head;
ListNode* pivot = head;
ListNode* ptr = head-> next;
ListNode lefthead = ListNode ( ) , * left = & lefthead;
ListNode righthead = ListNode ( ) , * right = & righthead;
while ( ptr) {
if ( ptr-> val <= pivot-> val) {
left-> next = ptr;
left = left-> next;
}
else {
right-> next = ptr;
right = right-> next;
}
ptr = ptr-> next;
}
left-> next = nullptr ;
right-> next = nullptr ;
lefthead. next = sortList ( lefthead. next) ;
righthead. next = sortList ( righthead. next) ;
ListNode* ret = ( lefthead. next ? lefthead. next : pivot) ;
ListNode* leftend = lefthead. next;
while ( leftend && leftend-> next) {
leftend = leftend-> next;
}
if ( leftend) {
leftend-> next = pivot;
}
pivot-> next = righthead. next;
return ret;
}
} ;
代码实现(六刷自解 DAY 5 Golang)
func Mergesort ( head * ListNode, left, right int ) * ListNode {
if left >= right {
return head
}
mid := ( ( right - left) >> 1 ) + left
ptr := head
for i := left; i < mid; i++ {
ptr = ptr. Next
}
next := ptr. Next
ptr. Next = nil
l := Mergesort ( head, left, mid)
r := Mergesort ( next, mid + 1 , right)
dummyhead := ListNode{ }
now := & dummyhead
for l != nil && r != nil {
if l. Val <= r. Val {
now. Next = l
l = l. Next
} else {
now. Next = r
r = r. Next
}
now = now. Next
}
if l != nil {
now. Next = l
}
if r != nil {
now. Next = r
}
return dummyhead. Next
}
func sortList ( head * ListNode) * ListNode {
ptr, length := head, 0
for ptr != nil {
length++
ptr = ptr. Next
}
return Mergesort ( head, 0 , length - 1 )
}