I am trying to understand this piece of code which basically performs insertion sort on a linked list. I am lost in this step.
cur->next = prev->next;
What is the role of this step?
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode* dummy = new ListNode();
ListNode* prev = dummy ;
ListNode* cur = head ;
ListNode* next;
while(cur!=NULL){
next = cur->next ;
while(prev->next!=NULL && prev->next->val <cur->val){
prev = prev->next;
}
cout<<"prev->next is "<<prev->next<<endl;
if(prev->next){
cout<<"val is "<<prev->next->val <<endl;
}
cur->next = prev->next;
prev->next = cur;
prev = dummy ;
cur = next;
}
return dummy->next ;
}
};