viewing paste recursive insertion_sort | C++

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
struct Node{
    int val;
    Node* next;
};
 
Node* insert(Node* p, Node* list)
{
    if(list == nullptr)
        return p;
    if(p->val > list->val)
    {
        list->next = insert(p,list->next);
        return list;
    }
    else
    {
        p->next = list;
        return p;
    }
}
 
Node* insertion_sort(Node *p)
{
    if(p == nullptr)
        return p;
    Node* sortedList = insertion_sort(p->next);
    p->next = nullptr;
    return insert(p,sortedList);
}
 
int main()
{
    return 0;
}
Viewed 1514 times, submitted by Streusel.