Tuesday, January 2, 2007

Are looking for specific interview question?!
There are so many simple ways of reversing a link list!! A simple systematic approach is to have these functions:
1. insert_head(List, element) returns new list by inserting element before head.
2. delete_head(List) returns head element after deleting from the list.

Assume the type this way:
typedef struct List_s {
struct List_s *next;
} List;
Then insert_head can be defined as:

List*
insert_head(List *l, List *e)
{
e->next = l;
return e;
}

delete_head would be:
List* delete_head(List **l)
{
List *e;
if (!l || !*l) return NULL;
e = *l;
*l = e->next;
return e;
}

Reverse would be a simple while loop:
List*
reverse_list(List *h)
{
List *n = NULL;
while (h) {
n = insert_head(n, delete_head(h));
}
return n;
}

1 comment:

Unknown said...

Great work .. We can hardly find good guys like you . Nice work man .. really helped me a lot .. It seems that you are a really intelligent guy.

All the best

Thats mee ...

Thats mee ...

About Me

Delhi, Delhi, India
I am an Electrical Engineering student of Indian Institute of Technology, Delhi. I am currently in 5th year of my Dual Degree course. By the end of this course I will proudly be called Masters in Information and Communication Technology. I have hard core programming interest and good at Networking Fundamentals. Enjoy surfing around Linux journals.