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
}
相关推荐
Learner__Q3 小时前
每天五分钟:滑动窗口-LeetCode高频题解析_day3
python·算法·leetcode
阿昭L4 小时前
leetcode链表相交
算法·leetcode·链表
小南家的青蛙6 小时前
LeetCode第1261题 - 在受污染的二叉树中查找元素
算法·leetcode·职场和发展
玖剹6 小时前
记忆化搜索题目(二)
c语言·c++·算法·leetcode·深度优先·剪枝·深度优先遍历
Jul1en_7 小时前
【算法】分治-归并类题目
java·算法·leetcode·排序算法
java修仙传7 小时前
力扣hot100:寻找旋转排序数组中的最小值
算法·leetcode·职场和发展
F_D_Z9 小时前
哈希表解Two Sum问题
python·算法·leetcode·哈希表
LYFlied10 小时前
【每日算法】LeetCode124. 二叉树中的最大路径和
数据结构·算法·leetcode·面试·职场和发展
小妖66611 小时前
力扣(LeetCode)- 93. 复原 IP 地址(JavaScript)
javascript·tcp/ip·leetcode
前端小白在前进13 小时前
力扣刷题:复原IP地址
tcp/ip·算法·leetcode