Leetcode 148. 排序链表(DAY 86) ---- Leetcode Hot 100


原题题目


在这里插入图片描述


代码实现(首刷自解)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
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 日后一定补)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
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];
    }
};

代码实现(三刷自解 归并链表)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
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)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
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++)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
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++)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
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)


/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
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)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Love 6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值