一、题目描述
提示:
链表中节点的数目在范围 [0, 5 * 104] 内
-105 <= Node.val <= 105
二、代码实现
(1)Python实现方法一
"""
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
"""
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def sortList(self, head):
"""
主要是使用归并的思路 先分再合
:type head: ListNode
:rtype: ListNode
"""
if head is None:
return None
return self.minute(head)
def minute(self, head):
"""
这个方法主要是实现分的操作
分的过程用快慢指针实现
:param head:
:return:
"""
if head.next is None:
return head
quick, slow, temp = head, head, head
while quick is not None and quick.next is not None:
temp = slow
slow = slow.next
quick = quick.next.next
temp.next = None # 这一步就是将整个链表从中间分开 失去这一步 后面将无限循环
i = self.minute(head)
j = self.minute(slow)
return self.Combined(i, j)
def Combined(self, i, j):
"""
这个方法主要实现合的操作
合的过程就是从i 和 j开始比较 就是从开头和中间开始比较 将两个相比小的排在head后
最后返回head即可
:param i:ListNode
:param j:ListNode
:return:
"""
TempNode = ListNode(0)
temp = TempNode
while i is not None and j is not None:
if i.val <= j.val:
temp.next = i
i = i.next
else:
temp.next = j
j = j.next
temp = temp.next
if i is not None:
temp.next = i
if j is not None:
temp.next = j
return TempNode.next
if __name__ == '__main__':
node = ListNode(4)
node.next = ListNode(2)
node.next.next = ListNode(1)
node.next.next.next = ListNode(3)
print(Solution().sortList(node))
(2)Python实现方法二
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def sortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
num_list = list()
p = head
while p:
num_list.append(p.val)
p = p.next
num_list.sort()
head_ = ListNode(-1)
p = head_
for i in num_list:
tmp = ListNode(int(i))
p.next = tmp
p = p.next
return head_.next
(3)Python实现方法三
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def sortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
head_ = ListNode(-1) #新增表头节点
head_.next = head
p = head.next #保留当前节点的第一个节点,用于比较
head.next = None
while p:
curr = p.next #保存下一个节点信息
q = head_ #设立头结点指针,每次都要循环判断
while q.next and q.next.val <= p.val:#一直循环找比当前头结点值小的节点
q = q.next
p.next = q.next #头插法插入节点
q.next = p #当前值比所有元素都大,所以要放在最后
p = curr #后移
return head_.next
(4)Java实现方法一
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
return mergeSort(head);
}
// 归并排序
private ListNode mergeSort(ListNode head){
// 如果没有结点/只有一个结点,无需排序,直接返回
if (head==null||head.next==null) return head;
// 快慢指针找出中位点
ListNode slowp=head,fastp=head.next.next,l,r;
while (fastp!=null&&fastp.next!=null){
slowp=slowp.next;
fastp=fastp.next.next;
}
// 对右半部分进行归并排序
r=mergeSort(slowp.next);
// 链表判断结束的标志:末尾节点.next==null
slowp.next=null;
// 对左半部分进行归并排序
l=mergeSort(head);
return mergeList(l,r);
}
// 合并链表
private ListNode mergeList(ListNode l,ListNode r){
// 临时头节点
ListNode tmpHead=new ListNode(-1);
ListNode p=tmpHead;
while (l!=null&&r!=null){
if (l.val<r.val){
p.next=l;
l=l.next;
}else {
p.next=r;
r=r.next;
}
p=p.next;
}
p.next=l==null?r:l;
return tmpHead.next;
}
}
(5)Java实现方法二
三、题目分析
参考文献
[1] https://leetcode-cn.com/problems/sort-list/
[2] https://www.bilibili.com/video/BV1FV411k7MQ?from=search&seid=14589989158538085268
[3] https://blog.csdn.net/sun_white_boy/article/details/87651131
[4] https://blog.csdn.net/hot7732788/article/details/103670891