Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
713 B
3
Indexable
Never
void list_reverse(node * & head_ptr){
    if (list_length(head_ptr) <= 1) return;
    if (list_length(head_ptr) == 2){
        std::cout << "activated" << std::endl;
        node * next = head_ptr -> link();
        next -> set_link(head_ptr);
        head_ptr -> set_link(nullptr);
        head_ptr = next;
    }
    else{
        node * current = head_ptr -> link();
        node * next;
        head_ptr -> set_link(nullptr);
        for (next = current -> link(); next!=nullptr; next = next -> link()){
            current->set_link(head_ptr);
            head_ptr = current;
            current = next;
        }
        current -> set_link(head_ptr);
        head_ptr = current;
    }
}
Leave a Comment