题解 | #在行列都排好序的矩阵中找指定的数#
牛牛的链表交换
http://www.nowcoder.com/practice/0e009fba6f3d47f0b5026b5f8b0cb1bc
#include<stdlib.h>
struct node{
int value;
struct node* next;
};
int main(){
int n;
int temp;
scanf("%d",&n);
if(n==0){
return 0;
}
if(n==1){
scanf("%d",&temp);
printf("%d",&temp);
}
struct node* head= (struct node*)malloc(sizeof(struct node));
struct node* pre = head;
struct node* next = head;
for(int i=0;i<n;i++){
next= (struct node*)malloc(sizeof(struct node));
scanf("%d",&temp);
next->value=temp;
pre->next=next;
if(i!=n-1){
pre=next;
}
}
struct node* p=head->next;
temp=p->next->value;
p->next->value=p->value;
p->value=temp;
temp=pre->value;
pre->value=next->value;
next->value=temp;
pre=head->next;
for(int i=0;i<n;i++){
printf("%d ",pre->value);
pre=pre->next;
}
}