代码改变世界

微软面试题:反序一个单向链表

2012-03-23 21:58  Rollen Holt  阅读(1067)  评论(0编辑  收藏  举报

反序一个单向链表

class Node {
Node* next;
}

// Return the new start after reversion. 
Node* ReverseList (Node* start) {  
}
#include <stdio.h> 

typedef struct snode{ 
char data; 
struct snode *next;}node; 

node *reserve(node *head){ 
node *p,*q,*r; 
p=head; 
q=p->next; 
while(q!=null){ 
r=q->next; 
q->next=p; 
p=q; 
q=r;} 
head->next=null; 

head=p; 

return head; 

} 

这个题目有变种:
对链表中部分节点进行反转操作,这些节点相隔k个:
0->1->2->3->4->5->6->7->8->9
k=2
8->1->6->3->4->5->2->7->0->9
注意1 3 5 7 9 位置是不变的。