91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

單鏈表快速排序

發布時間: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

岳阳市| 肇东市| 荃湾区| 乐昌市| 高雄县| 涞源县| 正宁县| 奎屯市| 安西县| 灵川县| 密山市| 南川市| 天津市| 濮阳县| 黔江区| 五家渠市| 化德县| 鹤峰县| 西安市| 高阳县| 石台县| 平舆县| 赤壁市| 修水县| 二连浩特市| 灵璧县| 平遥县| 池州市| 精河县| 康马县| 永城市| 岢岚县| 大城县| 毕节市| 永新县| 吐鲁番市| 库车县| 韶关市| 河东区| 台前县| 密云县|