版权声明: https://blog.csdn.net/qq_38386316/article/details/83152558
题目描述:
在 时间复杂度和常熟级空间复杂度下,对链表进行排序。
示例 1
- 输入: 4->2->1->3
- 输出: 1->2->3->4
示例 2
- 输入: -1->5->3->4->0
- 输出: -1->0->3->4->5
思路
由于两个链表都是乱序的,所以,自顶向下的归并排序用在这里非常合适了,使用快慢指针将其链表分割成两半,然后在进行归并。
代码
class Solution {
public ListNode sortList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode slow = head, fast = head, prev = null;
while(fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = null;
ListNode l1 = sortList(head);
ListNode l2 = sortList(slow);
return Merge(l1, l2);
}
public ListNode Merge(ListNode l1, ListNode l2) {
ListNode l = new ListNode(-1);
ListNode dummpy = l;
while(l1 != null && l2 != null) {
if(l1.val < l2.val) {
l.next = l1;
l1 = l1.next;
}else{
l.next = l2;
l2 = l2.next;
}
l = l.next;
}
if(l1 != null) {
l.next = l1;
}
if(l2 != null) {
l.next = l2;
}
return dummpy.next;
}
}
复杂度分析
- 时间复杂度:
- 空间复杂度:
完整代码
package leetcode148;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by 张超帅 on 2018/10/19.
*/
class ListNode {
int val;
ListNode next;
ListNode(int x){this.val = x;}
}
class Solution {
public ListNode sortList(ListNode head){
if(head == null || head.next == null) {
return head;
}
ListNode slow = head, fast = head, prev = null;
while(fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = null;
ListNode l1 = sortList(head);
ListNode l2 = sortList(slow);
return Merge(l1, l2);
}
public ListNode Merge(ListNode l1, ListNode l2){
ListNode l = new ListNode(-1);
ListNode cur = l;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
cur.next = l1;
l1 = l1.next;
}else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
if(l1 != null) {
cur.next = l1;
}
if(l2 != null) {
cur.next = l2;
}
return l.next;
}
}
public class leetcode148 {
public static int[] stringToArrays(String input){
input = input.trim();
input = input.substring(1, input.length() - 1);
if(input == null) {
return new int[0];
}
String[] parts = input.split(",");
int[] res = new int[parts.length];
for(int i = 0; i < parts.length; i ++) {
res[i] = Integer.parseInt(parts[i]);
}
return res;
}
public static ListNode stringToListNode(String input) {
int[] nodes = stringToArrays( input);
ListNode dummpy = new ListNode(-1);
ListNode cur = dummpy;
for(int node : nodes){
cur.next = new ListNode(node);
cur = cur.next;
}
return dummpy.next;
}
public static String listnodeToString(ListNode head){
if(head == null){
return "[]";
}
String res = "";
while(head != null){
res += head.val + ", ";
head = head.next;
}
return "[" + res.substring(0, res.length() - 2) + "]";
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = in.readLine()) != null) {
ListNode head = stringToListNode(line);
ListNode ret = new Solution().sortList(head);
System.out.println(listnodeToString(ret));
}
}
}