struct node  *reverse(struct node *head, int k )
{
struct node *prev = NULL;
struct node *current = head;
struct node *next = NULL;
int count = 0;

while(current && count < k) {
next = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}

/*** reverse remaining nodes recursively ***/
if (next != NULL) {
head->next = reverse(next, k);
}
 return prev;
}

eg
head = reverse(head, 2);
head = reverse(head, 3);
....


-
Azhar.


On Thu, Jun 2, 2011 at 2:09 PM, wujin chen <wujinchen...@gmail.com> wrote:

> how about this one?
>
> Node* reverseBy2(Node* head){
>     Node* p1 = head;
>     if(p1 == NULL)
>         return NULL;
>     Node* p2 = p1->next;
>     if(p2 == NULL)
>         return head;
>     Node* nextHead = p2->next;
>     p2->next = p1;
>     p1->next = reverseBy2(nextHead);
>     return p2;
> }
> [?]
>
> 2011/6/1 Shivaji Varma <shivaji...@gmail.com>
>
>> Hi,
>>
>> Please take a look at this link.
>>
>> http://mycsinterviewsexperiences.blogspot.com/
>>
>>
>> --
>> Shivaji
>>
>>
>> On Wed, Jun 1, 2011 at 6:26 AM, Anand <anandut2...@gmail.com> wrote:
>>
>>> Given a linked list of the form, 1->2->3->4->5->6, convert it into the
>>> form 2->1->4->3->6->5. Note that the nodes need to be altered and not the
>>> data contained in them
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

<<1E3.gif>>

Reply via email to