中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

單鏈表快速排序

發布時間:2020-08-15 12:39:02 來源:ITPUB博客 閱讀:168 作者: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

临高县| 山阴县| 达拉特旗| 平昌县| 甘肃省| 阳谷县| 介休市| 竹北市| 德钦县| 塔河县| 南投市| 孟村| 临泉县| 无极县| 崇义县| 呼图壁县| 建宁县| 永川市| 宜宾市| 封开县| 贞丰县| 昭通市| 韩城市| 潞西市| 镇平县| 舞钢市| 尼木县| 双峰县| 巴彦县| 天镇县| 石屏县| 天祝| 阿尔山市| 延边| 福州市| 宁海县| 北票市| 濮阳市| 台中县| 泸州市| 于都县|