sundeepblue
4/20/2014 - 6:52 PM

sort a linked list using bubble sort

sort a linked list using bubble sort

// below is not to swap the biggest to end. instead, 
// the smallest value will graduately swapped to the leftmost side


void sort_list_bubble(ListNode *head) {
    for(ListNode i=head; i->next; i=i->next) {
        for(ListNode j=i->next; j; j=j->next) {
            if(i->val > j->val)
                swap(i->val, j->val);
        }
    }
}