题解 | #牛牛的链表交换#

牛牛的链表交换

http://www.nowcoder.com/practice/0e009fba6f3d47f0b5026b5f8b0cb1bc

#include<stdio.h>
#include<stdlib.h>

struct List{
    int data;
    struct List *next;
};
void set_list(struct List *p,int n){
    int i=1;
    struct List *p1 = NULL;
    p1 = (struct List *)malloc(sizeof(struct List));
    p->next = p1;
    p1->next = NULL;
    scanf("%d",&(p1->data));
        while(n-1){
        set_list(p1, n-1);
        break;

    }
}
int main(){
    int n,m;
    scanf("%d",&n);
    struct List *head = NULL;
    head = (struct List*)malloc(sizeof(struct List));
    set_list(head, n);
    struct List *p = head->next;
    int temp;
    temp = p->data;
    p->data = p->next->data;
    p->next->data = temp;
    p = head;
    for(int i=1;i<=n-1;i++){
        p = p->next;
        if(i==n-1){
            temp = p->data;
            p->data = p->next->data;
            p->next->data = temp;
            break;
        }
    }
    p = head->next;
    for(int i=0;i<n;i++){
        printf("%d ",p->data);
        p=p->next;
    }
    return 0;
}
全部评论

相关推荐

07-15 14:14
门头沟学院 Java
7.10投递7.15感谢信
投递地平线等公司7个岗位
点赞 评论 收藏
分享
昨天 11:27
门头沟学院 Java
点赞 评论 收藏
分享
07-16 17:55
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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