Problem
Problem page: https://leetcode.com/problems/insertion-sort-list/
Topics: Linked List, Sort
Difficulty: Medium
Similar problems: (M) Sort List
Sort a linked list using insertion sort.
Analysis
Insertion sort on a linked list. I first saw someone's C++ solution online. It creates a new linked list and inserts the next node into it each time. I tried the same thing in Python, but it timed out. Later I changed it to sort the original linked list in place without creating a new one. I also added a check to avoid doing a lot of unnecessary work when the list is already sorted (because the first version timed out when the judge gave it a really long linked list that was already sorted -_-).
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-insertion-sort-list/
