here is my solution.
____________________________________________________________________
(Recursive)
struct list * reverse_ll( struct list * head)
{
struct list * temp = NULL;
if ( head == NULL)
return NULL;
if ( head->next == NULL )
return head;
temp = reverse_ll ( head->next );
head->next -> next = head;
head->next = NULL;
return temp;
}
int main()
{
...
head = reverse_ll(head);
...
}
____________________________________________________________________
(Iterative)
#define pointer_swap( a, b) ((int)(a))^=((int)(b))^=((int)(a))^=((int)(b))
struct list * reverse_ll( struct list * head)
{
struct list * temp = NULL;
if (head && head->next) {
temp = head->next;
head->next = NULL;
} else {
return head;
}
while (temp->next) {
pointer_swap(head, temp->next);
pointer_swap(head,temp);
}
temp->next = head;
//head = temp;
return temp;
}
____________________________________________________________________
Harish's code. Minimized version of my code.
#define pointer_swap(a,b) ((int)(a)) ^= ((int)(b)) ^= ((int)(a)) ^= ((int)(b))
struct list * reverse_ll(struct list * head)
{
struct list * temp = NULL;
while(head) {
pointer_swap(temp,head->next);
pointer_swap(temp,head);
}
return temp;
}
Subscribe to Post Comments [Atom]
July 2005 August 2005 October 2005 December 2005 March 2006 June 2006 July 2006 December 2006 February 2007 June 2007 March 2010 May 2010
Subscribe to Posts [Atom]