I want to do insert sort in a linked list without using dummy nodes.
This is my code. How can this be improved? Any input is appreciated.
public static Node insertSort(Node node){
Node sortedList = null;
while(node != null){
Node current = node;
node = node.next;
Node x;
Node previous = null;
for(x = sortedList; x != null; x = x.next){
if(current.value < x.value){
break;
}
previous = x;
}
if(previous == null){
current.next = sortedList;
sortedList = current;
}
else{
current.next = previous.next;
previous.next = current;
}
}
return sortedList;
}