Leetcode148-Sort_List

Sort_List

冒泡排序(超时了)

public ListNode sortList(ListNode head) {
        
        if(null == head)
            return head;
        
        int counter = 0;
        ListNode current = head;
        while (null != current.next) {
            current = current.next;
            counter++;
        }

        current = head;
        int pre;
        while (counter > 0) {
            for (int i = 0; i < counter; i++) {
                if (current.val > current.next.val) {
                    pre = current.val;
                    current.val = current.next.val;
                    current.next.val = pre;
                }
                current = current.next;
            }
            current = head;
            counter--;
        }
        return head;
        
    }

  

猜你喜欢

转载自www.cnblogs.com/Jomini/p/11735623.html
今日推荐