题解 | #在行列都排好序的矩阵中找指定的数#

牛牛的链表交换

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;
    }
    
    
        
    
}
全部评论

相关推荐

03-12 00:30
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务