背景
插入排序代码
插入排序就是从第一个节点开始,比较当前节点的左边的节点,找到合适的位置插入
用链表实现插入排序
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
##################################用来练习插入排序###########################
class Solution:
def length(l: ListNode):
"""链表长度"""
# 初始指针指向head
cur = l
count = 0
# 指针指向None 表示到达尾部
while cur is not None:
count += 1
# 指针下移
cur = cur.next
return count
def sortList(self, head: ListNode) -> ListNode: