温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

单链表快速排序

发布时间:2020-08-15 12:39:02 来源:ITPUB博客 阅读:169 作者:21ca 栏目:编程语言

点击(此处)折叠或打开

  1. public class LinkedListSortTest {
  2.     //bubble up
  3.     public static ListNode sortList(ListNode head) {
  4.         ListNode m = head;
  5.         ListNode tail = null;
  6.         while (m != tail && m.next != null) {
  7.             ListNode n = head;
  8.             while (n.next != null) {
  9.                 if (n.next.val < n.val) {
  10.                     swap(n, n.next);
  11.                 }
  12.                 n = n.next;
  13.             }
  14.             tail = n;
  15.             m = m.next;
  16.         }
  17.         return head;
  18.     }
  19.     
  20.     //quick sort
  21.     public static ListNode sortList2(ListNode head) {
  22.         ListNode next = head.next;
  23.         if (next == null) { // 1 element
  24.             return head;
  25.         } else if (next.next == null) { // 2 elements
  26.             if (head.val > next.val) {
  27.                 swap(head, next);
  28.             }
  29.             return head;
  30.         } else {
  31.             ListNode mid = getMidNode(head);
  32.             return merge(sortList(head), sortList(mid));
  33.         }
  34.     }


  35.     private static ListNode merge(ListNode m, ListNode n) {
  36. //        System.out.println("Merge " + m + " with " + n);
  37.         
  38.         ListNode head = new ListNode(0);
  39.         ListNode tail = head;
  40.         while (m != null && n != null) {
  41.             if (m.val < n.val) {
  42.                 tail.next = m;
  43.                 m = m.next;
  44.             } else {
  45.                 tail.next = n;
  46.                 n = n.next;
  47.             }
  48.             tail = tail.next;
  49.         }
  50.         if (m != null) {
  51.             tail.next = m;
  52.         }
  53.         if (n != null) {
  54.             tail.next = n;
  55.         }
  56. //        System.out.println("Merge result : " + head.next);
  57.         return head.next;
  58.     }

  59.     private static ListNode getMidNode(ListNode n) {
  60.         ListNode fast = n;
  61.         ListNode slow = n;
  62.         while (true){
  63.             if (fast.next != null) {
  64.                 fast = fast.next;
  65.                 if (fast.next != null) {
  66.                     fast = fast.next;
  67.                     slow = slow.next;
  68.                     continue;
  69.                 }
  70.             }
  71.             break;
  72.         }
  73.         ListNode mid = slow.next;
  74.         slow.next = null;
  75.         return mid;
  76.     }

  77.     private static void swap(ListNode n, ListNode m) {
  78.         int v = n.val;
  79.         n.val = m.val;
  80.         m.val = v;
  81.     }

  82.     public static void main(String[] args) {
  83.         ListNode head = new ListNode(0);
  84.         int i = 0;
  85.         ListNode n = head;
  86.         while (i++ < 20) {
  87.             n.next = new ListNode(i * 37 % 50);
  88. //            n.next = new ListNode(i);
  89.             n = n.next;
  90.         }

  91.         print(head);
  92.         print(sortList(head));
  93.         print(sortList2(head));

  94.     }

  95.     public static void print(ListNode n) {
  96.         while (n != null) {
  97.             System.out.print(n.val + " ");
  98.             n = n.next;
  99.         }
  100.         System.out.println();
  101.     }

  102. }

  103. class ListNode {
  104.     int val;
  105.     ListNode next;

  106.     ListNode(int x) {
  107.         val = x;
  108.     }
  109.     
  110.     public String toString() {
  111.         ListNode n = this;
  112.         StringBuilder sb = new StringBuilder("[");
  113.         while (n != null) {
  114.             sb.append(n.val + " ");
  115.             n = n.next;
  116.         }
  117.         sb.deleteCharAt(sb.length() - 1);
  118.         sb.append("]");
  119.         return sb.toString();
  120.     }
  121. }
来源于  https://leetcode.com/problems/sort-list/
148. Sort List 

Sort a linked list in O(n log n) time using constant space complexity.

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI