• leetcode148. 排序链表


    方法1:插入方法进行改进

    1. class Solution {
    2. public ListNode sortList(ListNode head) {
    3. /*
    4. 想法:设置两个指针first,last分别指向当前有序子链表的头和尾节点;
    5. 并遍历链表,当遍历到的节点值大于last的值时,就将该节点插入到有序子链表表尾
    6. 值小于first时,插入到子链表表头,处于二者中间时,就遍历进行插入
    7. */
    8. if(head == null)
    9. return null;
    10. ListNode first = head,last = head;
    11. ListNode p = head.next;
    12. head.next = null;
    13. while(p != null){
    14. ListNode temp = p.next;
    15. if(p.val >= last.val){
    16. //插入表尾
    17. last.next = p;
    18. p.next = null;
    19. last = p;
    20. }else if(p.val <= first.val){
    21. //插入表头
    22. p.next = first;
    23. first = p;
    24. }else{
    25. // 遍历进行插入
    26. for(ListNode q = first;q != last ;q = q.next){
    27. if(q.next.val > p.val){
    28. p.next = q.next;
    29. q.next = p;
    30. break;
    31. }
    32. }
    33. }
    34. p = temp;
    35. }
    36. return first;
    37. }
    38. }

    方法2:自顶向下的归并排序

    归并排序的时间复杂度问题:在每一层中进行寻找中间节点+有序链表进行两两合并都需要2n,而归并排序总共会进行logn层处理,因此最终的时间复杂度就是O(nlogn)。

    1. class Solution {
    2. public ListNode sortList(ListNode head) {
    3. /*
    4. 自顶向下的归并排序:首先寻找中间节点,在利用归并排序将当前需要排序的链表进行两两分别排序,
    5. 最后在通过合并两个有序链表的方式以及递归的方式进行排序。
    6. */
    7. if(head == null)
    8. return null;
    9. return sortSubList(head,null);
    10. }
    11. //将链表进行排序(tail指向)
    12. public ListNode sortSubList(ListNode head,ListNode tail){
    13. if(head.next == null)
    14. return head;
    15. if(head.next == tail){
    16. head.next = null;
    17. return merge(head,tail);
    18. }
    19. //先找到链表的中间节点
    20. ListNode slow = head,fast = head.next.next;
    21. while(fast != tail){
    22. slow = slow.next;
    23. fast = fast.next;
    24. if(fast != tail)
    25. fast = fast.next;
    26. }
    27. //将左边的子链表表尾指向空指针,右边子链表表尾本就是空指针
    28. ListNode subHead2 = slow.next;
    29. slow.next = null;
    30. ListNode head1 = sortSubList(head,slow);
    31. ListNode head2 = sortSubList(subHead2,tail);
    32. return merge(head1,head2);
    33. }
    34. //将两个子链表进行排序并合并返回合并后的链表头节点
    35. //判断是否到了尾,即是否到了空节点即可
    36. public ListNode merge(ListNode head1,ListNode head2){
    37. ListNode head = new ListNode(-1,null);
    38. ListNode temp = head,temp1 = head1,temp2 = head2;
    39. while(temp1 != null && temp2 != null){
    40. if(temp1.val < temp2.val){
    41. temp.next = temp1;
    42. temp1 = temp1.next;
    43. }else{
    44. temp.next = temp2;
    45. temp2 = temp2.next;
    46. }
    47. temp = temp.next;
    48. }
    49. while(temp1 != null){
    50. temp.next = temp1;
    51. temp1 = temp1.next;
    52. temp = temp.next;
    53. }
    54. while(temp2 != null){
    55. temp.next = temp2;
    56. temp2 = temp2.next;
    57. temp = temp.next;
    58. }
    59. return head.next;
    60. }
    61. }

     方法3:自底向上的归并排序

    1. class Solution {
    2. public ListNode sortList(ListNode head) {
    3. /*
    4. 自顶向下的归并排序:首先寻找中间节点,在利用归并排序将当前需要排序的链表进行两两分别排序,
    5. 最后在通过合并两个有序链表的方式以及递归的方式进行排序。
    6. */
    7. if(head == null)
    8. return null;
    9. //遍历链表获取链表长度
    10. int length = 0;
    11. ListNode p = head;
    12. while(p != null){
    13. length++;
    14. p = p.next;
    15. }
    16. ListNode hair = new ListNode(-1,head);
    17. for(int subLength = 1;subLength < length;subLength *= 2){
    18. //开始遍历链表,获取子链表,并两两合并
    19. ListNode pre = hair, cur = hair.next;
    20. while(cur != null){
    21. //获取一个的子链表
    22. ListNode head1 = cur;
    23. for(int i = 1;i < subLength && cur.next !=null;i++){
    24. cur = cur.next;
    25. }
    26. ListNode head2 = cur.next;
    27. cur.next = null;
    28. cur = head2;
    29. //再获取一个子链表
    30. for(int i = 1;i < subLength && cur!=null;i++){
    31. cur = cur.next;
    32. }
    33. if(cur != null){
    34. ListNode temp = cur.next;
    35. cur.next = null;
    36. cur = temp;
    37. }
    38. ListNode mergeResult = merge(head1,head2);
    39. //pre指针指向当前两个子链表的前面的一个节点
    40. pre.next = mergeResult;
    41. while(pre.next != null)
    42. pre = pre.next;
    43. }
    44. }
    45. return hair.next;
    46. }
    47. //将两个子链表进行排序并合并返回合并后的链表头节点
    48. //判断是否到了尾,即是否到了空节点即可
    49. public ListNode merge(ListNode head1,ListNode head2){
    50. ListNode head = new ListNode(-1,null);
    51. ListNode temp = head,temp1 = head1,temp2 = head2;
    52. while(temp1 != null && temp2 != null){
    53. if(temp1.val < temp2.val){
    54. temp.next = temp1;
    55. temp1 = temp1.next;
    56. }else{
    57. temp.next = temp2;
    58. temp2 = temp2.next;
    59. }
    60. temp = temp.next;
    61. }
    62. while(temp1 != null){
    63. temp.next = temp1;
    64. temp1 = temp1.next;
    65. temp = temp.next;
    66. }
    67. while(temp2 != null){
    68. temp.next = temp2;
    69. temp2 = temp2.next;
    70. temp = temp.next;
    71. }
    72. return head.next;
    73. }
    74. }
  • 相关阅读:
    [其他] ubuntu 22 上编译 ffmpeg
    @PropertySource读取自定义配置
    C语言基础知识
    重塑运维系统,跨越烟囱式建设的陷阱
    开源日志分析平台ELK实战应用
    前端常用操作(一)
    java毕业设计面向对象程序设计课程网站源码+lw文档+mybatis+系统+mysql数据库+调试
    观察者设计模式
    go-zero微服务实战系列(五、缓存代码怎么写)
    第29课 绘制原理图——放置电源端口
  • 原文地址:https://blog.csdn.net/grandZXW/article/details/137438800