Reputation: 1
I am trying to use bubble sort to sort a linked list. Instead of swapping the nodes, why can't I swap the values of the nodes and produce the desired output? The below code was my approach.
struct node
{
int data;
struct node *next;
};
void sort_ll(struct node *head){
struct node *p1 = head;
struct node *p2 = NULL;
while(p1->next!=NULL){
p2 = p1;
int temp;
while(p2->next!=NULL){
if(p2->data>p1->data){
temp = p2->data;
p2->data = p1->data;
p1->data = temp;
}
p2 = p2->next;
}
p1 = p1->next;
}
print_ll(p1);
}
Upvotes: 0
Views: 67