Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Wednesday, February 9, 2011

# Reverse a linked list

A common technical questions:

One simple way is to do two operations on a liked list:
-1- removing from the head of the 1st list
-2- inserting the element to the head of the new list


Element *reverse(Element *old_list)
{
    Element *new_list = NULL;

    while (old_list != NULL) {
        // Remove element from old list.
        Element *element = old_list;
        old_list = old_list->next;

        // Insert element in new list.
        element->next = new_list;
        new_list = element;
    }

    return new_list;
}



♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥

No comments:

Post a Comment