Sort List leetcode java

题目:

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

题解:

考虑到要求用O(nlogn)的时间复杂度和constant space complexity来sort list,自然而然想到了merge sort方法。同时我们还已经做过了merge k sorted list和merge 2 sorted list。这样这个问题就比较容易了。

不过这道题要找linkedlist中点,那当然就要用最经典的faster和slower方法,faster速度是slower的两倍,当faster到链尾时,slower就是中点,slower的next是下一半的开始点。

解决了这些问题,题目就很容易解出了。

代码如下:

1     public ListNode sortList(ListNode head) {
 2         if(head == null|| head.next == null)
 3             return head;
 4         ListNode slow = head, fast = head, firsthalf = head;
 5         while(fast.next!=null&&fast.next.next!=null){
 6             slow = slow.next;
 7             fast = fast.next.next;
 8         }
 9         ListNode secondhalf = slow.next;
10         slow.next = null;
11         
12         ListNode leftlist = null, rightlist =null;
13         if(firsthalf!=secondhalf){
14             leftlist = sortList(firsthalf);
15             rightlist = sortList(secondhalf);
16         }
17         return mergeTwoLists(leftlist, rightlist);
18     }
19     
20     public ListNode mergeTwoLists(ListNode leftlist, ListNode rightlist){
21         if(rightlist == null)
22             return leftlist;
23         if(leftlist == null)
24             return rightlist;
25         
26         ListNode fakehead = new ListNode(-1);
27         ListNode ptr = fakehead;
28         while(rightlist!=null&&leftlist!=null){
29             if(rightlist.val<leftlist.val){
30                 ptr.next = rightlist;
31                 ptr = ptr.next;
32                 rightlist = rightlist.next;
33             }else{
34                 ptr.next = leftlist;
35                 ptr = ptr.next;
36                 leftlist = leftlist.next;
37             }
38         }
39         
40         if(rightlist!=null)
41             ptr.next = rightlist;
42         if(leftlist!=null)
43             ptr.next = leftlist;
44         
45         return fakehead.next;
46     }

Sort List leetcode java

时间: 2024-08-26 21:32:45

Sort List leetcode java的相关文章

Sort Colors leetcode java

题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, an

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Combination Sum II leetcode java

题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

Scramble String leetcode java

题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choo

Subset leetcode java

题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3

Subset II leetcode java

题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solution

Combination Sum leetcode java

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) w

Merge Interval leetcode java

题目: Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 题解: 这道题主要难点是改写Comparator. Comparator接口定义了两个方法:compare( )和equals( ).这里给出的compare( )方法按顺序比较了两个元素: int comp

Anagrams leetcode java

题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 这道题看所给的字符串数组里面有多少个是同一个变形词变的.这道题同样使用HashMap来帮助存老值和新值,以及帮忙判断是否是变形词. 首先对每个string转换成char array然后排下序,HashMap里面的key存sort后的词,value存原始的