zoukankan      html  css  js  c++  java
  • 148. 排序链表

     归并排序

    class Solution {
        public ListNode sortList(ListNode head) {
            if(head == null || head.next == null) return head;
            ListNode fast = head,slow = head;
            while(fast.next != null && fast.next.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            ListNode next = slow.next;
            slow.next = null;
            ListNode left = sortList(head);
            ListNode right = sortList(next);
            return merge(left,right);
        }
        public ListNode merge(ListNode l, ListNode r) {
            if(l == null) return r;
            if(r == null) return l;
            if(l.val < r.val) {
                l.next = merge(l.next,r);
                return l;
            }
            r.next = merge(l,r.next);
            return r;
        }
    }
  • 相关阅读:
    git merge
    google platform
    http tutorial
    DS,AA tree
    Java,Hashtable
    java,Object
    Pumping lemma for regular languages
    Pumping lemma
    Context-free grammar
    Formal language
  • 原文地址:https://www.cnblogs.com/yonezu/p/13385717.html
Copyright © 2011-2022 走看看