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
}
相关推荐
Pniubi2 小时前
力扣54.螺旋矩阵
算法·leetcode·矩阵
利威尔xu13 小时前
Go map 底层崩溃:3 个 goroutine 并发写同一张 map,fatal error 崩掉整个服务
golang·源码·map·并发·后端开发
Rain的Java大神之路14 小时前
LeetCode 37 解数独面试攻略:回溯 + 位掩码 + MRV,从暴力到毫秒级求解
java·后端·leetcode
Pniubi15 小时前
力扣73 矩阵置零
算法·leetcode·矩阵
文丑颜不良啊1 天前
LeetCode2261~2280题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2241~2260题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2321~2340题目解析
java·leetcode
利威尔xu2 天前
2026-11-08-Go-defer-panic-recover面试
面试·golang
文丑颜不良啊2 天前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z2 天前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode