Recursive sort of linked list

Hi, I'm trying to implement a recursive sorting of a singly linked list, so here it goes>
Algorithm:
1) find largest value in the list
2) put it on Head
3) start function again, but without that node
1
2
3
4
struct Node {
    int data;
    Node* next;
}Node;

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
  Node* sort_ll(Node* head) {
    if(head == NULL)
        return head;  //should be base case
    else
    {
        int max = 0;
        Node* tmp = head;
        Node* to_move = head;

        while(tmp != NULL)  //find max value and store it into to_move
        {
            if(tmp->data > max) {
                max = tmp->data;
                to_move = tmp;
            }

            tmp = tmp->next;
        }

        tmp = head;

        if(to_move == head)  //if it's already in head position
            return sort_ll(head->next);

        while(tmp->next != to_move)  //find it
            tmp = tmp->next;

        tmp->next = tmp->next->next;  //move it to head
        to_move->next = head;
        head = to_move;

        return sort_ll(head->next);  //call function again
    }
}



My code doesn't "remember" old values, it just prints out minimum, because that is whats left in the end.
How can I fix this please?
Topic archived. No new replies allowed.