Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Insertion Sort List

XPChallenge Points: 20
levelLevel: Medium

You are given a linked list of numbers. Sort the list in ascending order using the insertion sort method and return the sorted list.


How insertion sort works:
• Start with the first number as a sorted list.
• Take the next number from the list and place it in the correct position in the sorted part.
• Repeat until all numbers are sorted.

Example 1:

Input: head = [3,1,4,2]

Output: [1,2,3,4]

Explanation:

Step by step, the list is sorted from [3] → [1,3] → [1,3,4] → [1,2,3,4].

Example 2:

Input: head = [7,2,5,3,1]

Output: [1,2,3,5,7]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result