-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q_SortLinkedList.java
105 lines (99 loc) · 2.88 KB
/
Q_SortLinkedList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* @author wmy
* @date 2021/6/11 16:46
*/
import java.util.*;
/*
描述
给定一个无序单链表,实现单链表的排序(按升序排序)。
示例1
输入:
[1,3,2,4,5]
复制
返回值:
{1,2,3,4,5}
复制
*/
public class Q_SortLinkedList {
// /**
// * 第一种方法:快排
// *
// * @param head ListNode类 the head node
// * @return ListNode类
// */
// public ListNode sortInList(ListNode head) {
// // write code here
// if (head == null || head.next == null) {
// return head;
// }
// ArrayList<Integer> temp = new ArrayList<>();
// while (head != null) {
// temp.add(head.val);
// head = head.next;
// }
// temp.sort(Comparator.naturalOrder());
// head = null;
// ListNode auxiliary = null;
// for (Integer integer : temp) {
// if (auxiliary == null) {
// head = auxiliary = new ListNode(integer);
// } else {
// auxiliary.next = new ListNode(integer);
// auxiliary = auxiliary.next;
// }
// }
// return head;
// }
/**
* 第二种方法:归并排序
*
* @param head ListNode类 the head node
* @return ListNode类
*/
public ListNode sortInList(ListNode head) {
// write code here
if (head == null || head.next == null)
return head;
//使用快慢指针找到中点
ListNode slow = head, fast = head.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
//分割为两个链表
ListNode newList = slow.next;
slow.next = null;
//将两个链表继续分割
ListNode left = sortInList(head);
ListNode right = sortInList(newList);
ListNode lhead = new ListNode(-1);
ListNode res = lhead;
//归并排序
while (left != null && right != null) {
if (left.val < right.val) {
lhead.next = left;
left = left.next;
} else {
lhead.next = right;
right = right.next;
}
lhead = lhead.next;
}
//判断左右链表是否为空
lhead.next = left != null ? left : right;
return res.next;
}
public static void main(String[] args) {
Q_SortLinkedList app = new Q_SortLinkedList();
ListNode ln1 = new ListNode(3);
ListNode ln2 = new ListNode(2);
ListNode ln3 = new ListNode(1);
ln1.next = ln2;
ln2.next = ln3;
ListNode res = app.sortInList(ln1);
while (res != null) {
System.out.print(res.val + " ");
res = res.next;
}
}
}