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
}
相关推荐
pan_junbiao20 分钟前
Go 语言基本数据类型
java·前端·golang
codeejun9 小时前
每日一Go·MySQL-24、死锁排查与解决方案
开发语言·mysql·golang
利威尔xu14 小时前
Go context取消传播3层:信号传不到第3层goroutine,泄漏拖垮
后端·golang
Sakura_j_h_17 小时前
TypeScript 7.0 换 Go 编译器后,我先在 6.0 清完默认值
javascript·golang·typescript·vite·前端工程化·tsc
旖旎夜光1 天前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
木井巳2 天前
【动态规划: 子序列问题】最长递增子序列的个数
java·算法·leetcode·动态规划·推荐算法
码途行者2 天前
每天10分钟学Go:第23课 协程(goroutine)
开发语言·后端·golang
利威尔xu2 天前
2026-11-25-Go-panic-recover面试
面试·golang
码途行者2 天前
每天10分钟学Go:第24课 通道-协程之间的传送带
开发语言·后端·golang
Navigator_Z2 天前
LeetCode //C - 1326. Minimum Number of Taps to Open to Water a Garden
c语言·算法·leetcode