Insertion Sort List

Question (LC.147)

Sort a linked list using insertion sort.

Example

3 -> 2 -> 5 -> null
2 -> 3 -> 5 -> null

Analysis

Insertion Sort
dummy node

for each node in the linked list
    insert the node into the sorted position
end for

return dummy.next;

Insert
stop when the next node.value > newNode.value
Node temp = travel.next;
travel.next = newNode;
newNode.next = temp;

Code

Last updated