Golang | Leetcode Golang题解之第147题对链表进行插入排序

题目:

题解:

Go 复制代码
func insertionSortList(head *ListNode) *ListNode {
    if head == nil {
        return nil
    }
    dummyHead := &ListNode{Next: head}
    lastSorted, curr := head, head.Next
    for curr != nil {
        if lastSorted.Val <= curr.Val {
            lastSorted = lastSorted.Next
        } else {
            prev := dummyHead
            for prev.Next.Val <= curr.Val {
                prev = prev.Next
            }
            lastSorted.Next = curr.Next
            curr.Next = prev.Next
            prev.Next = curr
        }
        curr = lastSorted.Next
    }
    return dummyHead.Next
}
相关推荐
GalaxyPokemon31 分钟前
LeetCode - 148. 排序链表
linux·算法·leetcode
chao_7893 小时前
链表题解——环形链表 II【LeetCode】
数据结构·leetcode·链表
海奥华25 小时前
go中的接口返回设计思想
开发语言·后端·golang
岁忧6 小时前
LeetCode 高频 SQL 50 题(基础版)之 【高级字符串函数 / 正则表达式 / 子句】· 上
sql·算法·leetcode
eachin_z6 小时前
力扣刷题(第四十九天)
算法·leetcode·职场和发展
飞川撸码9 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
_Itachi__17 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
roman_日积跬步-终至千里17 小时前
【Go语言基础【14】】defer与异常处理(panic、recover)
golang
孔令飞18 小时前
Kubernetes 节点自动伸缩(Cluster Autoscaler)原理与实践
ai·云原生·容器·golang·kubernetes