Leetcode version 2

148. Sort List

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

题意:排序链表

思路I:merge sort

复杂度分析:时间复杂度O(nlgn),空间复杂度O(1)

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9
10 // merge sort version
11 class Solution {
12     public ListNode sortList(ListNode head) {
13         if (head == null || head.next == null) {
14             return head;
15         }
16         ListNode middle = getMiddle(head);
17         ListNode rightHead = middle.next;
18         middle.next = null;
19         ListNode left = sortList(head);
20         ListNode right = sortList(rightHead);
21         return merge(left, right);
22     }
23     public ListNode getMiddle(ListNode head) {
24         if (head == null || head.next == null) {
25             return head;
26         }
27         ListNode slow = head;
28         ListNode fast = head.next;
29         while (fast != null && fast.next != null) {
30             slow = slow.next;
31             fast = fast.next.next;
32         }
33         return slow;
34     }
35     public ListNode merge(ListNode left, ListNode right) {
36         ListNode dummy = new ListNode(0);
37         ListNode cur = dummy;
38         while (left != null && right != null) {
39             if (left.val <= right.val) {
40                 cur.next = left;
41                 left = left.next;
42             } else {
43                 cur.next = right;
44                 right = right.next;
45             }
46             cur = cur.next;
47         }
48         if (left != null) {
49             cur.next = left;
50         }
51         if (right != null) {
52             cur.next = right;
53         }
54         return dummy.next;
55     }
56 }

思路II:quick sort

复杂度分析:时间复杂度O(nlgn),空间复杂度O(1)

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9
10 // quick sort version
11 class Solution {
12     public ListNode sortList(ListNode head) {
13         if (head == null || head.next == null) {
14             return head;
15         }
16         ListNode middle = getMiddle(head);
17         ListNode leftDummy = new ListNode(0), leftTail = leftDummy;
18         ListNode middleDummy = new ListNode(0), middleTail = middleDummy;
19         ListNode rightDummy = new ListNode(0), rightTail = rightDummy;
20         while (head != null) {
21             if ( head.val < middle.val) {
22                 leftTail.next = head;
23                 leftTail = leftTail.next;
24             } else if (head.val == middle.val) {
25                 middleTail.next = head;
26                 middleTail = middleTail.next;
27             } else {
28                 rightTail.next = head;
29                 rightTail = rightTail.next;
30             }
31             head = head.next;
32         }
33         leftTail.next = null; middleTail.next = null; rightTail.next = null;
34         ListNode left = sortList(leftDummy.next);
35         ListNode right = sortList(rightDummy.next);
36         return merge(merge(left, middleDummy.next), right);
37     }
38     public ListNode getMiddle(ListNode head) {
39         if (head == null || head.next == null) {
40             return head;
41         }
42         ListNode slow = head;
43         ListNode fast = head.next;
44         while (fast != null && fast.next != null) {
45             slow = slow.next;
46             fast = fast.next.next;
47         }
48         return slow;
49     }
50     public ListNode merge(ListNode left, ListNode right) {
51         ListNode dummy = new ListNode(0);
52         ListNode cur = dummy;
53         while (left != null && right != null) {
54             if (left.val <= right.val) {
55                 cur.next = left;
56                 left = left.next;
57             } else {
58                 cur.next = right;
59                 right = right.next;
60             }
61             cur = cur.next;
62         }
63         if (left != null) {
64             cur.next = left;
65         }
66         if (right != null) {
67             cur.next = right;
68         }
69         return dummy.next;
70     }
71 }

时间: 2024-08-29 10:16:00

Leetcode version 2的相关文章

【Leetcode】:Compare Version Numbers

题目地址:https://oj.leetcode.com/problems/compare-version-numbers/ 题目描述: Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings ar

LeetCode:Compare Version Numbers - 比较版本号

1.题目名称 Compare Version Numbers(比较版本号) 2.题目地址 https://leetcode.com/problems/compare-version-numbers/ 3.题目内容 英文:Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. 中

LeetCode 278 First Bad Version

LeetCode  278 First Bad Version // Forward declaration of isBadVersion API. bool isBadVersion(int version); int firstBadVersion(int n) { int start=1, end=n; while(start < end) { int mid=start+(end-start)/2; if(isBadVersion(mid)) end=mid; else start=m

[LeetCode][JavaScript]First Bad Version

First Bad Version You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versi

LeetCode:First Bad Version - 第一个坏版本

1.题目名称 First Bad Version(第一个坏版本) 2.题目地址 https://leetcode.com/problems/first-bad-version/ 3.题目内容 英文: You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality c

【leetcode 字符串处理】Compare Version Numbers

[leetcode 字符串处理]Compare Version Numbers @author:wepon @blog:http://blog.csdn.net/u012162613 1.题目 Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume

【一天一道LeetCode】#165. Compare Version Numbers.md

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: https://leetcode.com/problems/compare-version-numbers/ Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 <

[C++]LeetCode: 59 Compare Version Numbers

题目: Compare two version numbers version1 and version1. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . ch

[LeetCode] 165. Compare Version Numbers 比较版本数

Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character.The . characte