I'm trying to write a code to sort a linked list containing integers but its not working how I thought it would based on my reasoning I worked out for it with pencil and paper. Instead of traversing through the list, it compares the first pair of values, deletes the second value in the list and returns the remainder of the list. My method code is:
//typedef Node * ListType;
void insertionSort(ListType &list) {
ListType p = list;
Node * curr;
if(p == NULL || p->next == NULL){
return;
}
while(p != NULL){
curr = p->next;
if(curr == NULL){
break;
}
if(p->data > curr->data){
p->next = curr->next;
curr->next = p;
}
p = p->next;
}
}
Say, for instance I start with a list: 5 2 3 4 The output I get after calling this method on this list is: 5 3 4
I'm not comfortable with pointers. Any help is appreciated!